participate


Java Programming [Archive] - Util to auto-generate getters and setters...
<<   Back to Forum  |   Give us Feedback
This topic has 12 replies on 1 page.
samlewis23
Posts:443
Registered: 11/27/00
Util to auto-generate getters and setters...   
Jul 24, 2002 2:40 AM

 
Does anyone know of a utility that automatically generate getters and setter classes from a list of variable names???

Might stop me getting RSI!
 
Abuse
Posts:2,819
Registered: 7/10/01
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:02 AM (reply 1 of 12)  (In reply to original post )

 
i gave up on gets/sets about 2weeks after my lecturer introduced them to us :/

if a var can be modified, then make it public. Though adding a get/set method does provide encapsulation, it also requires more typing, bloats code and is also a fraction slower.

Sometimes gets/sets serve a purpose, but most of the time theyre just a waste of time.

Its quite funny watching a newbie programmer start writing a class, they identify the classes required attributes, then write 200lines of gets and sets before they even consider tackling the 'hard' bit[s] of the class :]

rob,

Answer supplied by Friends of the Water Cooler. Please suggest to forum admin via Discuss the JDC Web Site forum that off-topic threads be supported in some manner.
 
Kayaman
Posts:2,521
Registered: 7/18/01
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:09 AM (reply 2 of 12)  (In reply to #1 )

 
Most IDEs can autogenerate them.

off-topic threads be supported in some manner <

Off topic discussion is moved to http://developer.java.sun.com/developer/ChatClient.html
 
thomas.kellerer
Posts:745
Registered: 12/9/99
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:29 AM (reply 3 of 12)  (In reply to original post )

 
NetBeans (and its Sun sibling Forte for Java) does that.

Thomas
 
samlewis23
Posts:443
Registered: 11/27/00
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:34 AM (reply 4 of 12)  (In reply to #1 )

 
i gave up on gets/sets about 2weeks after my lecturer
introduced them to us :/

if a var can be modified, then make it public. Though
adding a get/set method does provide encapsulation, it
also requires more typing, bloats code and is also a
fraction slower.

Sometimes gets/sets serve a purpose, but most of the
time theyre just a waste of time.

Its quite funny watching a newbie programmer start
writing a class, they identify the classes required
attributes, then write 200lines of gets and sets
before they even consider tackling the 'hard' bit[s]
of the class :]

rob,

Answer supplied by Friends of the Water Cooler.
Please suggest to forum admin via Discuss the JDC Web
Site forum that off-topic threads be supported in some
manner.


I really dont have a problem with getters and setters (apart from having to write them), the jre is optimized for gets and sets so the performance argument doesnt hold (or cool) any water.
Forcing people to use get and set methods means you can do whatever you want to the inner workings of the class as long as you leave the get/set methods there and they still give the expected result. I try not to make variables public unless they are static and final.
 
samlewis23
Posts:443
Registered: 11/27/00
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:38 AM (reply 5 of 12)  (In reply to #3 )

 
Thanks all... I will download NetBeans.
 
bopen
Posts:49
Registered: 5/16/00
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:41 AM (reply 6 of 12)  (In reply to #1 )

 
i gave up on gets/sets about 2weeks after my lecturer
introduced them to us :/

Giving up on gets/sets is a mistake... take it from an EXPERIENCED programmer.

if a var can be modified, then make it public. Though
adding a get/set method does provide encapsulation, it
also requires more typing, bloats code and is also a
fraction slower.

Adding get/set methods provide more then just the encapsulation. It provides you easier debug not to mention easier way to read the code.

Sometimes gets/sets serve a purpose, but most of the
time theyre just a waste of time.

If you think set/get is a waste of time your attitude will get you into trouble. Consider code with a full set of public variables in a 'complex' system (well, lets just say 1500 classes).

During development a bug is discovered and you realize that the bug is due to a change in a specific variable. How do you, quickly and simply, find out what classes are changing the variable. It could be anywhere; but by having a get and set method for that variable you could add a simple code like "new Exception().printStackTrace();" into the set method and get a trace when the bug happens. This way you would know within secondes what object is changing the variable making the debugging easy.

What if you would like to override a class and to something before or after a variable is manipulated? This would be impossible if all variables are public. You will loose all control of you code.

There are many more reasons for adding the get/set methods but it will take me all day to write them all here.

I say: "have all variables protected, GET OFF YOUR ***, and add the 200 lines of code" if not for you then for the one that later will be using or fixing the code.

Its quite funny watching a newbie programmer start
writing a class, they identify the classes required
attributes, then write 200lines of gets and sets
before they even consider tackling the 'hard' bit[s]
of the class :]

What do think of code guidlines that are forced by most software companies? This is more important then most NEWBIES think; wait a few years and you will get the point..

What about comments? Do you find them funny and useless? hope you don't...for your sake.

Thinking it funny that people take the time and effort to make their code more readable, understandable, accessable, flexible and over all more pretty makes you the newbie.

It scares me to think that the new breed of programmers will think it funny to write GOOD code.

bopen, bwise, bbetter...
 
mchan0
Posts:3,573
Registered: 9/18/97
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 3:54 AM (reply 7 of 12)  (In reply to #6 )

 
'ALL' variables should be private.
think twice about setters.
minimize getters.
there are exceptions to the 'rules'
http://forum.java.sun.com/thread.jsp?forum=31&thread=186158
bflexible
 
macrules2
Posts:2,023
Registered: 2/7/02
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 4:42 AM (reply 8 of 12)  (In reply to #1 )

 
Its quite funny watching a newbie programmer start
writing a class, they identify the classes required
attributes, then write 200lines of gets and sets
before they even consider tackling the 'hard' bit[s]
of the class :]

Its even funnier to watch a smart a[/b]ssed kid who thinks he knows it all, trying to change code when a desgn change requires him to change the type of an attribute on a class.

And even funnier still when hundreds/thousands of lines of code are added redunantly to format values which have been received raw from a class.

And its ******* hillarious watching him attempting to untangle the web of closly coupled classes.

They may be a pain in the *** to write, and they are "dirty" up your code, but they are essential for encapsulation.
 
Abuse
Posts:2,819
Registered: 7/10/01
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 5:44 AM (reply 9 of 12)  (In reply to #6 )

 
i gave up on gets/sets about 2weeks after my
lecturer
introduced them to us :/

Giving up on gets/sets is a mistake... take it from an
EXPERIENCED programmer.

you assume 2 much. Uni was a long time ago 4 me.


if a var can be modified, then make it public.
Though
adding a get/set method does provide encapsulation,
it
also requires more typing, bloats code and is also
a
fraction slower.

Adding get/set methods provide more then just the
encapsulation. It provides you easier debug not to
mention easier way to read the code.

Encapsulation encapsulates the idea of ezier debuggin :]

gets/sets do not automatically give you code readability, and badly named gets/sets can detract from readability.


Sometimes gets/sets serve a purpose, but most of
the
time theyre just a waste of time.

If you think set/get is a waste of time your attitude
will get you into trouble. Consider code with a full
set of public variables in a 'complex' system (well,
lets just say 1500 classes).

ok, you've applied my philosophy to your field, now let me apply yours to mine.

I write games for Java enabled mobile phones(J2ME MIDP1.0), on this platform code size (and memory usage) is a SERIOUS concern.
FYI. the Nokia 6310i mobile phone has approx. 140k of heap, and a jar size limited of 30k.

EVERY line of code has to be optimal, in both space and time,

The cost of gets/sets; inheritance; interfaces and all the other wonderful OO design features of java are serious performance inhibitors, and consequently are used only when absolutly necessary.


During development a bug is discovered and you realize
that the bug is due to a change in a specific
variable. How do you, quickly and simply, find out
what classes are changing the variable. It could be
anywhere; but by having a get and set method for that
variable you could add a simple code like "new
Exception().printStackTrace();" into the set method
and get a trace when the bug happens. This way you
would know within secondes what object is changing the
variable making the debugging easy.

don't write buggy code ;] (that was a j/k btw)

btw, im curious how exactly do u realise that the bug is related to a specific variable? gets/sets help debugging, but they are not the magic bullet of debugging techniques.


What if you would like to override a class and to
something before or after a variable is manipulated?
This would be impossible if all variables are public.
You will loose all control of you code.

you are still argueing a different point to me - the abstraction of gets/sets do serve a purpose, however they also impose a cost.


There are many more reasons for adding the get/set
methods but it will take me all day to write them all
here.

I say: "have all variables protected, GET OFF YOUR
***, and add the 200 lines of code" if not for you
then for the one that later will be using or fixing
the code.

Its quite funny watching a newbie programmer start
writing a class, they identify the classes required
attributes, then write 200lines of gets and sets
before they even consider tackling the 'hard'
bit[s]
of the class :]

What do think of code guidlines that are forced by
most software companies? This is more important then
most NEWBIES think; wait a few years and you will get
the point..

my point here, is that training programmers to follow guidelines before you have taught them the fundamentals of problem solving is futile.

What about comments? Do you find them funny and
useless? hope you don't...for your sake.

no, all good code should be commented. But I have to admit, I don't waste time commenting code as i write it, i find it slows down my coding. However I will always go back and comment any code if it is to be used by some1 else.


Thinking it funny that people take the time and effort
to make their code more readable, understandable,
accessable, flexible and over all more pretty makes
you the newbie.

hmm, unprovoked flaming - now whos the newbie :/


It scares me to think that the new breed of
programmers will think it funny to write GOOD code.

bopen, bwise, bbetter...

What frustrates me, is why good design always means slower performance.
It shouldn't, and until Java progresses to the point where the runtime cost of good design is not significant, I will still regard Java as a primitive language.
 
Abuse
Posts:2,819
Registered: 7/10/01
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 5:50 AM (reply 10 of 12)  (In reply to #2 )

 
Most IDEs can autogenerate them.

off-topic threads be supported in some manner <

Off topic discussion is moved to
http://developer.java.sun.com/developer/ChatClient.html

damn the firewall @work!! :(

(and no, 4some reason the http doesn't work either)
 
mchan0
Posts:3,573
Registered: 9/18/97
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 6:04 AM (reply 11 of 12)  (In reply to #8 )

 
an object with only variables, getters and setters has no 'class' at all, merely a glorified struct. one way to use this object is call a getter method, operate on the returned object; or instantiate a new object and call the setter method.

There is no encapsulation, the internals are exposed and any changes to the internals, breaks all the classes using it because of the coupling.
the operations occuring outside of the object, can be refactored inside the object with a suitably named 'behavioural' method name. Then get rid of the getter and setter for that variable. This gives 'better' encapsulation and also endows the object with some behaviour and 'class'.
 
Abuse
Posts:2,819
Registered: 7/10/01
Re: Util to auto-generate getters and setters...   
Jul 24, 2002 6:09 AM (reply 12 of 12)  (In reply to #11 )

 
an object with only variables, getters and setters has
no 'class' at all, merely a glorified struct. one way
to use this object is call a getter method, operate on
the returned object; or instantiate a new object and
call the setter method.

There is no encapsulation, the internals are exposed
and any changes to the internals, breaks all the
classes using it because of the coupling.
the operations occuring outside of the object, can be
refactored inside the object with a suitably named
'behavioural' method name. Then get rid of the getter
and setter for that variable. This gives 'better'
encapsulation and also endows the object with some
behaviour and 'class'.

yup, totally agree
 
This topic has 12 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums