Are getters and setters object oriented?
11–20 of 35 posts
Re: Are getters and setters object oriented?
#12There's a more interesting point to be made about getters and setters than the article offers. In my opinion, getters and setters are a little stinky because by nature they expose internal attributes of the class, even if they wrap them up in method calls. See http://stackoverflow.com/questions/565095/java-are-getters-a... .
The entire point of a getter/setter is to hide the internal attributes of the class behind a layer you can later change, and therefore they aren't "actually" exposing the internals, they just happen to be implemented that way at the moment but may later change. However, the problem is that they are a code smell, specifically of a designer that doesn't really "get" OO. If you really have it all set up correctly, it sh…
It's a problem when people assume that getters and setters must always occur in pairs. They shouldn't! Getters can expose whatever is needed to communicate the object's external identity and state. Setters are mostly a code smell. You should pass whatever you need to a constructor and use behavioural methods to mutate the internal state after construction. For instance, a Car class can have a getSpeed(), but shouldn't (in a clean design) have a setSpeed(). It should have stepOnTheGas() or increaseSpeed().
Getters and setters make classes into little more than verbose structs.
Obviously everything I've said is aimed mainly at Java-ish languages, which was the focus of the OP's comment.
Re: Are getters and setters object oriented?
#13Having standard getters and setters enabled things such as Spring (which began as a poor man's enterprise container) and have culminated in this http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/pdf/w....
Re: Are getters and setters object oriented?
#14Earlier quoted context omitted.
The entire point of a getter/setter is to hide the internal attributes of the class behind a layer you can later change, and therefore they aren't "actually" exposing the internals, they just happen to be implemented that way at the moment but may later change. However, the problem is that they are a code smell, specifically of a designer that doesn't really "get" OO. If you really have it all set up correctly, it sh…
You almost always need to have a client object observe the state of an instance - clients need to marshall it for the network or log some information about it, etc. So getters make sense. It's a problem when people assume that getters and setters must always occur in pairs. They shouldn't! Getters can expose whatever is needed to communicate the object's external identity and state. Setters are mostly a code smell. Y…
In pure usage of the language, clearly if a class with an immutable public element is appropriate, you may use it.
If you only need a getter for your immutable property (and are not writing classes that are destined by management by a container) then do so. It is perfectly OK.
However, if your Java object is ever going to be treated generically by a tool, container, external system (serialization), etc., then you need to "expose" your internal structure. After all, the user of the object may in fact be tasked with "initializing" the object. (In other words, imagine you are the author of a visual editor for Java programs.)
Before criticizing the good engineers at Sun, remember that annotations were not available back in the early days. Today it is in fact quite possible to design a new specification for Java Beans that delegates ALL code generations to annotation processors and gives you the developer a set of annotations to sprinkle on your otherwise uncluttered Java code.
Re: Are getters and setters object oriented?
#15Using property overloading to implement getters/setters in PHP is terrible practice. It's slow, makes the code hard to manage and introduces unexpected behavior. function __set($property, $value){ if ($property == 'speed'){ // ... } } Just write the damn setter or no setter at all but please don't use property overloading for this! function setSpeed($value) {}
Re: Are getters and setters object oriented?
#16Don't just change it, because the change seems "cleaner". You are breaking convention in a major way, and this will surely lead to worse code in the future.
Re: Are getters and setters object oriented?
#17And ruby has a nice solution for this. It allows methods ending in = to act as setters clean overrides and provides methods (attr_accessor and friends) to generate vanilla getters and setters for you. You can access instance variables via instance_variable_get but that's not recommended.
Re: Are getters and setters object oriented?
#18Languages are based on widely understood conventions. If it looks like I'm writing to or from a variable, then let that be what happens, because that's how 98% of programmers will understand it to be. Don't just change it, because the change seems "cleaner". You are breaking convention in a major way, and this will surely lead to worse code in the future.
Case in point: In Ruby, foo.bar and foo.bar= are never property accessors, it's just that sometimes there exists code such that foo.bar returns @bar and foo.bar= sets @bar.
Re: Are getters and setters object oriented?
#19Re: Are getters and setters object oriented?
#20Languages are based on widely understood conventions. If it looks like I'm writing to or from a variable, then let that be what happens, because that's how 98% of programmers will understand it to be. Don't just change it, because the change seems "cleaner". You are breaking convention in a major way, and this will surely lead to worse code in the future.
Counter-examples: python properties, ruby (=, ? and normal methods), C# properties (and events), probably any message-passing language (via 'name' or ('name', 'newvalue')), lots of other languages mentioned in other comments here...
How is not using setValue, getValue "breaking converntion in a major way" then? Java, PHP, Javascript and some others do it that way. This is their convention - true. But it's far from "an OOP convention" - mainly since OOP doesn't define the syntax in any way - just some properties of the environment.