Skip to main content
Object Calisthenics 80 XP · 8 min

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.

Showing
Ad (728×90)

Why this matters

Getters and setters are public fields wearing a disguise. When the caller reads the balance, adds to it, and writes it back, the business rule (the deposit amount must be positive, the balance must never go negative) is scattered across every call site. When the object owns that rule entirely — via a deposit() method — you can never break it from outside. This is the core of the "Tell, Don't Ask" principle.

Code Challenge

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

Key takeaway

Replace getters and setters with methods that describe intent — deposit(), withdraw(), promote(). Business rules live inside the object and can never be bypassed from outside.

Done with this lesson?

Mark it complete to earn XP and track your progress.