You are writing a comment about Tip Your Hat To Accessors And Mutators, here is a quick summary:
Accessors and mutators are often the scourge of new programmers, mostly because they appear to be needless lines of code that get in the way of writing code that <strong>does something</strong>. I think thats mostly because no one has ever introduced them to just why accessors and mutators can be so useful.
You are responding to this comment written by Peter on March 17th 2008, 04:57.
In defense of the unnamed student[1], accessors and mutators are rather stupid… in Java.
This is yet another thing that bloats Java code. Every attribute of a well designed library has to have getter and setter methods for every public or protected field, whether there's any logic therein or not. This is worth the tradeoff for all of the excellent uses for them that you've given here.
However, it doesn't have to be this way. Ruby and recent versions of Python make this sort of thing transparent, letting you expose a property at one point, and then later adding a getter, a setter, or both, all without changing the interface for the class in the slightest. You get the functionality, a clean API, and tight elegant code.
1) Ok, I am writing this defense in part because I'm afraid that the unnamed student is me. I do enjoy spouting my mouth off, and nothing pisses me off quite like boilerplate. (At least, boilerplate that humans have to touch or look at.)
Please be aware that comment forms go stale after one hour.
Comments may make use of LifeFlow MarkDown. Raw html will be escaped.
Quick Introduction to LifeFlow MarkDown Syntax
A highlighted code block:
Other common languages work as well: scheme, python, java, html, etc.
Other markdown syntax: