Hacker News new | past | comments | ask | show | jobs | submit login

> If you're writing a plain data class, why on earth would you write getters and setters? Just make your fields public and be done with it.

When doing pure OOP, objects should only communicate via methods, period, it's called state encapsulation and is one of the core principle of OOP, the second being polymorphism.

> I will be forever perplexed by the idea of getters and setters (and this extends to C#s syntax sugar). I have no idea what problem they solve.

Then you don't understand encapsulation.




For arguments sake you could say foo.bar = "baz" syntactic suggar for setBar("baz") sure you now cant add logic to the set method without introducing a real setBar method (and therefore breaking your public interface) but for immutable final DTO / POD style classes that don't have setters just use a public final field and be done with it

There's a difference between understanding encapsulation and being pragmatic




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: