LWN.net Logo

Getters and setters

Getters and setters

Posted Feb 6, 2011 23:30 UTC (Sun) by man_ls (subscriber, #15091)
In reply to: C for anything by ncm
Parent article: LCA: Lessons from 30 years of Sendmail

You have keep the Java monkeys away, though, or you'll spend your life writing Get and Set functions, and calls to them.
That comment is a bit cruel, but it's right on target. I have been wondering for the last 5 years what all those getters and setters were buying us. On one hand you were supposed to use them instead of public attributes because you could change the way to access the particular attribute later on. On the other, you were not supposed to do weird things in getters or setters since it might be considered as a bad practice. Even simple encapsulation of another object can be frowned upon in certain circles, and it certainly does not help understand the code.

I turned to public attributes a few years ago and never regretted it. You know, I'm not against the occasional access method, and separating methods with side effects from methods that return values is an excellent practice. But mandating a whole level of indirection just in case, just for the sake of it? Why?

Then I found out that not everything is an object, and I stopped wondering. My Python skills have improved since I understood that simple truth.


(Log in to post comments)

Getters and setters

Posted Feb 11, 2011 12:26 UTC (Fri) by mattthecat (guest, #72858) [Link]

The monkeys are those who use Java that write get and set functions.
The few of us that recognize Java without the added c**p are not monkeys.

"Don't got to use no stinkin' getters or setters"

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds