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.