Re: classes question



Wojtek Bok wrote:
Thomas Weidenfeller wrote:
Typically, the method changes the state as a side effect of doing
something, but it has (unfortunately) also become very popular to
provide methods which just change an object's state, but doing nothing else.

This statement with:

In Java, methods just changing an object's internal variable/state are
called setters. Methods just reporting an object's state/variable are
called getters.

confuses me.

Getters and setters are there to change the state. Sometimes they also do some ancillary processing, but mostly they change the values of private variables.

So in the first statement you label this as a bad thing, while in the second statement you endorse it. So which is it?

Not trying to start a flame-war here. Just confused.

I exclusively use getters/setters for all access to object variables (state change). This has saved me where I needed a setter to also change another state variable. A trivial example which assumes that the setting of the name is done once, whereas retrieving the full name happens many times:

Original:
public void setGivenName(String name)
{
ivGivenName = name;
}

Changed:
public void setGivenName(String name)
{
ivGivenName = name;
ivFullName = ivGivenName + " " + ivSurname;
}
I think the question that you should be asking yourself is why does one object *need* access to another object's state. Ideally the object itself should do all processing that involves its own state - that's what Thomas was talking about in his first statement. If getters and setters are needed, it implies that some object is performing processing involving another object's state, and perhaps indicates that the design needs refactoring. Getters & setters are a violation of encapsulation and should be avoided.

Just my tuppence-worth

Mark
.



Relevant Pages

  • Re: OO in Python? ^^
    ... > getters and setters in Java? ... getters, then it makes sense to write setters and getters. ... insurance against you changing the private interface. ... > a hack in Python (common, "hiding" an implementation detail by prefixing ...
    (comp.lang.python)
  • Re: "Getter" methods no good?
    ... The fear is that if you have getters and setters on an object, then other objects are going to extract the private data of the object, do some processing on it and write it back. ... there are many cases where instance variables of an object are fundamental to the object's behavior and you need to be able to set them or get them. ... BTW, I wrote a physics simulator myself and found that the best algorithm to solve the physics equations was to extract the positions and velocities from the particles, store them into one big vector, run Adaptive Runge Kutta on the vector, then write all the data back. ...
    (comp.lang.smalltalk)
  • Re: Holub on getters/setters again
    ... In this post by DanielT, I find that DanielT is blatantly guilty of ... >> A class with lots of getters and setters may be an indication of ... >> and setters. ... I'm outraged at both DanielT and RCM, and any one of good conscience ...
    (comp.object)
  • Re: How to write Smart Python programs?
    ... Do not write getters and setters. ... In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. ... In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. ...
    (comp.lang.python)
  • Re: How to write Smart Python programs?
    ... "Getters and setters are evil. ... Do not write getters and setters. ... def getx: return self._x ...
    (comp.lang.python)