Try not to use getters and setters

Avoid getters and setters. Why? Because they break encapsulation. If you need them, it’s usually a sign of bad code, “code smell”. In some cases, they can be useful, but in general, avoid them. If you think you need them, think about how else you can avoid them. Perhaps using “friend” as long as they’re behind the same architectural boundary interface. Or perhaps the class itself could do the work instead of exposing it’s privates to the world. Using getters and setters is almost equivalent to just making the members “public”.

Leave a Reply

You must be logged in to post a comment.