Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player data clump #30

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

raaaahman
Copy link

Player Data Clump

The issue

Game:l50-53 shows 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::players array 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 the Game::add() and Game::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::purses and Game::inPenaltyBox.

With all the data access encapsulated, we can nowmove these fields to the Player class. The Game::players will now hold references to Player instances 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 PlayersList class scope, where the player removal feature belongs.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant