Share via


Object Calisthenics: Rule 7: Don't use any classes with more than two instance variables

This is another aspect of keeping your entities small I think. Classes with a lot of instance variables tend to have more than one responsibility. This rule may also help you create a more generic design if used correctly. For example consider you want a class representing the names (first, last and middle) of a person. Since this would be three strings you choose to represent it as GivenNames and LastName where GivenNames is another class with a list of names. Suddenly you support any number of first/middle names. So a more generic solution. End even if you don't do this and let GivenNames only have two names (First and Middle) this can be changed more easily in the future.

All that said, remember that this is a rule intended for practice, not necessarily a good thing to always use in all code.