Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question about some long java code that has getters/setters

Reply
Thread Tools

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

 
 
lewbloch
Guest
Posts: n/a
 
      07-26-2011
On Jul 26, 11:20*am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> Gene Wirchenko <ge...@ocis.net> writes:
> >>>What is wrong with something like:
> >>>FilePrinter r=new FilePrinter();
> >>>// General Options
> >>>r.SetPrinter("\\Boojum");
> >>>r.SetCopies(1);
> >>>r.SetDoubleSided(true);
> >>Before I can answer this, I need to know, whether there are
> >>setters in the code above. How do you define setter ? Is a
> >> setter any one-argument method, whose name begins with Set ?

> >In my example, yes. *In general, no.

>
> * There is nothing wrong with one-argument methods whose
> * name begins with Set (except that common Java naming
> * style is to use lower-case method names).
>
> >>(To apply my own definition, I would need to see the
> >>documentation of a method, to judge whether it is a setter .)

> >Are you going by whether the setter uncritically sets the value
> >or whether it checks the value?

>
> * According to
>
> http://c2.com/cgi/wiki?AccessorsAreEvil
>
> * * * accessors are methods that let you read and write the
> * * * value of an instance variable of an object .
>
> * My personal definition is similar:
>
> * * * An accessor is any method whose contract contains a
> * * * reference to an instance variable.
>
> * Since I do not know whether your methods set the value of an
> * instance variable, I do not know, whether they are setters
> * in the sense of my definition. I would need to be able to
> * read their contract (documentation) to learn about this.
>
> * When their contract (documentation) does not contain such
> * references, encapsulation is not broken.


Strictly speaking an accessor need not reference an instance variable
of the class that owns the accessor. It only needs to give the
appearance of doing so. Gene Wirchenko makes the excellent point that
accessors can and often should include sanity logic, i.e., logic to
preserve invariants.

--
Lew
 
Reply With Quote
 
 
 
 
lewbloch
Guest
Posts: n/a
 
      07-26-2011
On Jul 26, 1:34*pm, Patricia Shanahan <p...@acm.org> wrote:
> On 7/26/2011 12:15 PM, Stefan Ram wrote:
>
>
>
>
>
>
>
>
>
> > Gene Wirchenko<ge...@ocis.net> *writes:
> >>> Since I do not know whether your methods

>
> > * *(insert according to their contract , here)

>
> >>> * * * * * * * * * * * * * * * * * * * * *set the value of an
> >>> instance variable, I do not know, whether they are setters
> >>> in the sense of my definition. I would need to be able to
> >>> read their contract (documentation) to learn about this.
> >> Read my example contract above. *It does not mention an instance
> >> variable, but any reasonable implementation that I am likely to do
> >> would use one.

>
> > * *Ok, then - to me - it is not a setter .

>
> >> They set something. *Whether it is an instance variable or not is
> >> irrelevant to their use.

>
> > * *That is even another reason not to make references to instance
> > * *variables in the contract of a method.

>
> > * *A Setter to me is a method with a contract like:

>
> > * * * * ... sets the private field "alpha" to the argument value.

>
> > * *Your method does not set a field but a property of the object.
> > * *Its contract does not allow a party to deduce with
> > * *certainity something about the private fields of the object.
> > * *Therefore, it does not break encapsulation.

>
> We have two possible definitions of "setter", with corresponding
> definitions for "getter".
>
> 1. A method whose contract specifies setting an instance variable.
>
> 2. A method whose contract specifies setting a logical property of the
> object.
>
> To me, it seems wasteful to use an nice, short, simple term for
> something that should *never* exist, when it could equally well be used
> for something that is actually useful, so I prefer the second definition.


+1

--
Lew
 
Reply With Quote
 
 
 
 
Jukka Lahtinen
Guest
Posts: n/a
 
      07-27-2011
lewbloch <> writes:
> On Jul 26, 2:19*am, Jukka Lahtinen <jtfjd...@hotmail.com.invalid>


>> lewbloch <lewbl...@gmail.com> writes:
>> > Chad wrote:
>> >> The following code, which is taken from one of my school books,
>> > Really, don't use this book. *Get a good book.


>> TOP told us it's a school book. When at school, were you allowed to
>> choose the books yourself?


> Yes, as long as I also chose the books the teacher specified.

...
> If you are committed to learning and the book is bad, you get a good
> book and learn it independently of whatever the professor mandates.


Of course, a dedicated student may read good books in addition to what
the school requires. But the point is, they often can't just ditch the
book required for the studies, no matter how bad it is.
They often need to do exercises from the given book, and the book, no
matter how bad it is, probably makes it easier to follow the lectures
and summarize what has already been taught.

--
Jukka Lahtinen
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      07-27-2011
Patricia Shanahan <> writes:
>To me, it seems wasteful to use an nice, short, simple term for
>something that should *never* exist, when it could equally well be used
>for something that is actually useful, so I prefer the second definition.


For a property of an object, you could also use the term
»property« (no surprise intended here). You have made the point
yourself, that a method to modify a property should be accompanied
by a method to read a property, but where this is not the case,
we could use »read-only property« or »write-only property«.

This also would have the advantage that it would use the terms
in the way already established in the field.

 
Reply With Quote
 
lewbloch
Guest
Posts: n/a
 
      07-27-2011
On Jul 27, 5:56*am, Jukka Lahtinen <jtfjd...@hotmail.com.invalid>
wrote:
> lewbloch <lewbl...@gmail.com> writes:
> > On Jul 26, 2:19 am, Jukka Lahtinen <jtfjd...@hotmail.com.invalid>
> >> lewbloch <lewbl...@gmail.com> writes:
> >> > Chad wrote:
> >> >> The following code, which is taken from one of my school books,
> >> > Really, don't use this book. Get a good book.
> >> TOP told us it's a school book. When at school, were you allowed to
> >> choose the books yourself?

> > Yes, as long as I also chose the books the teacher specified.

> ..
> > If you are committed to learning and the book is bad, you get a good
> > book and learn it independently of whatever the professor mandates.

>
> Of course, a dedicated student may read good books in addition to what
> the school requires. But the point is, they often can't just ditch the
> book required for the studies, no matter how bad it is.
> They often need to do exercises from the given book, and the book, no
> matter how bad it is, probably makes it easier to follow the lectures
> and summarize what has already been taught.


I avoided suggesting dropping the bad book for exactly that reason. I
see you agree with me that the good books should be used in addition
to what the school requires.

The question is whether you want to learn the material or merely pass
the course. It is common to get misinformation even at university,
and incumbent on the student to make sure they aren't fooled.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
Is there any one who has been working with java for a long long time? Amanda Java 26 11-11-2006 12:43 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57