Overusing Getters and Setters
You'll learn when accessor methods add needless indirection instead of true encapsulation.
The Problem with Automatic Accessors
Getters and setters can provide controlled access to object properties, but they're often overused as a "just in case" pattern. If your getter simply returns a value and your setter simply assigns it without any validation, logic, or side effects, you're adding complexity without benefit.
Think of it like hiring a receptionist whose only job is to hand you mail sitting on the desk. They add a step but no value—you could just grab it yourself.
When Getters/Setters Add Nothing
// Needless indirection
class User {
#name;
#email;
getName() { return this.#name; }
setName(value) { this.#name = value; }
getEmail() { return this.#email; }
setEmail(value) { this.#email = value; }
}
// Simpler and clearer
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
}
When They're Actually Useful
Accessors make sense when they provide real value:
class User {
#email;
get email() {
return this.#email;
}
set email(value) {
// Validation adds real value
if (!value.includes('@')) {
throw new Error('Invalid email');
}
this.#email = value.toLowerCase();
}
}
Here, the setter validates and normalizes input—that's genuine encapsulation, not just ceremony.
The Anti-Pattern
Creating getters/setters for every property "because OOP" makes your code verbose and harder to read. Use direct property access by default. Only introduce accessors when you need validation, computed values, lazy loading, or other real logic.
Key Takeaway: Getters and setters should enforce business rules or add meaningful behavior—not just wrap direct property access in extra syntax.