Skip to main content
Object Calisthenics 70 XP · 7 min

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.

Showing
Ad (728×90)

Why this matters

Every extra instance variable beyond two is a sign of a hidden concept waiting to become its own class. A class with seven fields like firstName, lastName, street, city, country, email, and phone is really four concepts — Name, Address, Location, and Contact — jammed into one. Decomposing them reveals the domain structure and dramatically increases cohesion: every method in a two-variable class uses both variables, which is the definition of maximum cohesion.

Code Challenge

Study the messy code, try to refactor it, then reveal the clean version.

Key takeaway

Every extra instance variable beyond two is a hidden concept waiting to become its own class. Decompose until each class has at most two fields — the resulting hierarchy will reveal the true structure of your domain.

Done with this lesson?

Mark it complete to earn XP and track your progress.