martinsson / BugsZero-Kata

Practice Designing code for less bugs
86 stars 131 forks source link

Solution 2-6 players: create one constructor for every possible number of players #13

Open martinsson opened 6 years ago

martinsson commented 6 years ago

It used to be possible to construct a game with less than two players and with more than six players however at runtime things did not work out. There was also no validation that the game was in a playable state. The user had to come up with the idea to call the isPlayable method.

In this version it will not compile for anything other than 2-6 players. The Game class can only be instantiated it the correct way, and the user gets immediate feedback from the compiler on any usage errors.

Granted this type of technique becomes awkward when the number of constructors grows big, in those cases we revert back to a more traditional strategy of runtime validations in the constructor.