Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Writing 20 or 30 get/set methods is bad' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Writing 20 or 30 get/set methods is bad
Authored by: Anonymous on Aug 06, '02 07:56:31AM

Encapsulation (or -for others who perhaps don't know-preventing access to the internals of a class from outside and requiring accessor functions) is a good thing.
However you should think carefully before immediately implementing a large number of accessor functions. Do you really need access to all of the functions of the class? Could you perhaps bundle related values into objects of some kind?
For example
int box_height;
int box_width;
int box_length;
int box_color;
into a class called box which is itself accessed from your main class
and accessed with product.box().height()
You still have each accessor function but now you can reuse the box class elsewhere. Perhaps you have a function volume() which might compute the volume of a box. And you could use the box class in another program altogether.

removals.storageCrate().volume()

Though color() would probably always be brown in this case(?)

It isn't that good an example. :-)

If you can break up the program into smaller units they are more likely to be resuseable. Which of course is one of these goals of OO programming :-)

I once took a lecture series at university called Software Engineering-objects and components where the lecturer recommended no more than 4 main responsibilities for any class. If you had more then you were supposed to refactor the application and seperate stuff out more.
I couldn't say whether I manage this in the code that I write. :-)

However regardless of all of this, this tool might come in handy. I'm not writing so much java at the moment.More demand for other things.
Thanks for thinking it up.

Angus



[ Reply to This | # ]