Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getters and Setters: Code smell, Necessary Evil, or Can't Live Without Them [duplicate]

Tags:

oop

Possible Duplicate:
Allen Holub wrote “You should never use get/set functions”, is he correct?

Is there a good, no, a very good reason, to go through all the trouble of using getters and setters for object-oriented languages? What's wrong with just using a direct reference to a property or method? Is there some kind of "semantical coverup" that people don't want to talk about in polite company? Was I just too tired and fell asleep when someone walked out and said "Thou Shalt Write Copious Amounts of Code to Obtain Getters and Setters"?


Follow-up after a year:

It seems to be a common occurrence with Java, less so with Python. I'm beginning to wonder if this is more of a cultural phenomena (related to the limitations of the language) rather than "sage advice". As I do not program in Java (currently by choice) I cannot make that assessment.

The current (current being as of this writing 2010-03-22) -1 question score is complete for-the-lulz as far as I am concerned. It's interesting that there are specific questions that are downvoted, not because they are "bad questions", but rather, because they hit someone's raw nerve.

So let's get to the nut of the matter. I repeat myself:

What's wrong with just using a direct reference to a property or method?

And here's the unwritten corollary:

Are we so undisciplined as programmers that we can't keep our hands off of things that are clearly marked "no touchy"?

like image 503
Avery Payne Avatar asked Jun 21 '09 01:06

Avery Payne


1 Answers

Here is Allen Holub (who is brilliant) on the matter. He goes into much more detail on this subject in Holub on Patterns. Some things require public getters and setters like serialization and patterns like the Data Transfer Object pattern. In general, I think necessary evil since your application becomes convoluted when you don't use them.

like image 120
JP Alioto Avatar answered Oct 22 '22 19:10

JP Alioto