Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6572

Re: A question about some long java code that has getters/setters

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: A question about some long java code that has getters/setters
References <d94f5902-8b2e-484f-8095-9ce034e916d4@t8g2000prm.googlegroups.com> <1672e2f1-a963-4fcf-b651-41b69432c9d7@p29g2000pre.googlegroups.com> <m21uxehzrj.fsf@softwarematters.org> <j0kvh1$1eq$1@dont-email.me> <Getters-and-Setters-20110726023724@ram.dialup.fu-berlin.de>
Message-ID <5ewXp.62484$eG6.58250@newsfe09.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-07-26 06:59 -0300

Show all headers | View raw


On 11-07-25 09:42 PM, Stefan Ram wrote:
> markspace <-@.> writes:
>> On 7/25/2011 1:27 PM, Patrick May wrote:
>>> It encourages a style of programming where
>>> objects have too much knowledge about each other.
>> This is an interesting idea.
> 
>   I would not call an »interesting idea«, what is the
>   common standard of object-oriented programming teaching.
>   Obviously, Getters and Setters break encapsulation.
> 
>   To be specific, there are classic articles such as
> 
> http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
> 
>   or
> 
> http://c2.com/cgi/wiki?AccessorsAreEvil
> 
>   .
>   However, Java is not an fully object-oriented programming
>   language and therefore, for Java programming one should
>   adopt a Java style (not an OOP style). Getters and Setters
>   are parts of the Java culture and of the Java Beans
>   specification. Therefore, it is appropriate to use them in
>   Java programming whenever deemed advantageous.
> 
Wikipedia has two definitions for encapsulation; the c2.com article is
(legitimately IMO) questioning excessive use of accessors under the
terms of the second Wikipedia definition. Note the word "questioning":
that article has a bunch of nuanced arguments on both sides. It's in the
context of this second definition specifically that Patrick advanced his
objections.

It's not black and white, obviously. Even if we are restricting our
discussion to simple accessors (the getters and setters are simple
assignments or returns) then it's clearly still possible that the nature
of the particular object is such that no access can set the state to an
invalid one.

And as others have pointed out, it's entirely a situation-specific
matter as to whether a profusion of accessors is even a code-smell: you
want to use JPA entities and view-model classes with care, but their
very nature argues for exposing their guts.

But I do agree with some that in much teaching these ideas about
accessors do not get advanced. Students are therefore left with the
impression that _routine_ use of accessors is OK. What's worse is that
they don't even get the scoop on writing proper accessors (e.g.
mutability considerations), nor on object consistency or class
invariants. _This_ is the actual smell, not the considered use of accessors.

Getters and setters are not just "Java style". It's an issue for every
OOP language. Real-world objects describe both data (state) and
behaviour, and run the spectrum from [all state-no behaviour] to [no
state-all behaviour]. Practical considerations mean that not all
behaviour will have all of its required state in the same class: hence
accessors.

AHS

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A question about some long java code that has getters/setters Chad <cdalten@gmail.com> - 2011-07-22 16:12 -0700
  Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-22 19:28 -0400
  Re: A question about some long java code that has getters/setters Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-22 23:31 +0000
  Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-22 16:51 -0700
  Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-23 09:02 -0700
    Re: A question about some long java code that has getters/setters blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-23 17:41 +0000
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-23 11:42 -0700
      Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-23 17:59 -0400
        Re: A question about some long java code that has getters/setters Steve Sobol <sjsobol@JustThe.net> - 2011-07-23 23:39 -0700
          Re: A question about some long java code that has getters/setters blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-25 19:44 +0000
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:03 -0700
    Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-23 19:26 -0400
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:00 -0700
    Re: A question about some long java code that has getters/setters Patrick May <patrick@softwarematters.org> - 2011-07-25 16:27 -0400
      Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-25 15:14 -0700
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-25 15:56 -0700
        Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-25 17:00 -0700
          Re: A question about some long java code that has getters/setters Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-25 21:17 -0400
      Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-25 16:50 -0700
        Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-25 18:30 -0700
          Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-25 18:41 -0700
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:05 -0700
          Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-26 10:48 -0700
            Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-26 11:56 -0700
              Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-26 13:34 -0700
                Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 14:21 -0700
            Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 14:20 -0700
        Re: A question about some long java code that has getters/setters Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-25 21:35 -0400
        Re: A question about some long java code that has getters/setters Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-07-26 06:59 -0300
    Re: A question about some long java code that has getters/setters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-07-26 12:19 +0300
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:08 -0700
        Re: A question about some long java code that has getters/setters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-07-27 15:56 +0300
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-27 14:14 -0700

csiph-web