Object Calisthenics
Nine rules to transform how you write object-oriented code.
9 lessons
Use First-Class Collections
Any class containing a collection should have no other member variables — wrapping collections gives filtering, sorting, and rules a semantic and cohesive home.
Keep All Entities Small
No class over 50 lines, no package over 10 files — small classes fit on one screen, force focus, and naturally organize into cohesive packages.
Don't Abbreviate
Abbreviations mask deeper problems — if a name is too long to write out, the method has too many responsibilities. Names should be one or two clear words.
Don't Use the Else Keyword
Replace if/else with guard clauses and early returns — else is usually a sign of unclear flow that can be simplified into a straightforward sequence.
Don't Use Getters/Setters/Properties
Tell, don't ask — instead of getting data to make decisions outside the object, tell the object what to do and let it use its own data.
One Dot per Line
Based on the Law of Demeter — don't reach through an object's internals. Ask the object to do the work instead of navigating its parts.
One Level of Indentation per Method
Each method should do one thing at one level of abstraction — nested control structures are a sign to extract a method.
No Classes with More Than Two Instance Variables
Limiting state to two instance variables dramatically increases cohesion — if you need more, decompose into a hierarchy of collaborating objects.
Wrap All Primitives and Strings
A raw int or string has no domain meaning — wrapping it in a class like Money or Email lets the compiler enforce intent and gives behavior a natural home.