Open
Conversation
Maybe it is dependent on the environment?
Replace player number by player reference
…e current player into a dedicated collection class This allows to move some of our test one level below
A bug was found while extending test coverage
Introduce the PlayerInfoIterator object to query data from a PlayersList without introducing mutability weaknesses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Player Data Clump
The issue
Game:l50-53shows an example of Data Clump: these are the players related data, and each index relates to a single player. Such indices need to be consistent across the arrays, which can eventually lead to bugs.This commit introduces a new feature: now players can leave a game while running.
As outlined in the tests from this commit, removing a player from the
Game::playersarray will mismatch the remaining players with the recorded data.While this particular bug could be fixed by setting back the correct data in the
Game::remove()method, now any changes on these data, or inclusion of a new type of data (for example, players may be assigned a color), would then need to be performed both in theGame::add()andGame::remove()methods. This might not be obvious to a new programmer joining the project.Introducing a data holding class
The solution would be to group all the data referring to a same player in a dedicated data holding instance, representing this player.
To perform this operation safely, a first step is to encapsulate fields for
Game::players,Game::places,Game::pursesandGame::inPenaltyBox.With all the data access encapsulated, we can nowmove these fields to the
Playerclass. TheGame::playerswill now hold references toPlayerinstances in order to access this data.Fixing the remove method
This refactor could have been stopped there, but the following commits outlines two new bugs that may occur when implementing the
Game::remove()method:-If a player leaves, the current player may be changed
We can benefits from our players being instances here to use a reference to the current player, instead of an integer referring to its position in an array.
Cleaning up
This could also bring an end to this refactoring, although, the encapsulation of these change of the current player offers us a good opportunity to enforce separation of concerns, by moving all the code related to selecting the current player in a dedicated collection class
This in turn gives us the opportunity to level down all of the written tests to the
PlayersListclass scope, where the player removal feature belongs.