Re: How to emluate "properties" in Java?
On 02/25/2010 06:26 PM, Roedy Green wrote:
I strenuously disagree. A verbose program is a buggy program hard to
proofread and wasteful of time to compose. Getters and setters are
infantile syntax.
There is some virtue in verbosity: it means you can identify patterns
more easily.
Right now, in Java, "a.b = 5" is always going to set a field in the
object A to 5. If you had properties, the same syntax could also mean to
call a function with an argument of 5, which may or may not set a field
to 5. It could also just decide to sneer in your face for the heck of it.
Let's compare:
a.b = 5;
a.setB(5);
You've saved a grand total of 2 characters at the cost of hiding a
method call, which impairs the ability of a reader to quickly glance
over code and see what is happening. You've also made syntax ambiguous,
since whether or not `b' is a field access or a method call depends on
the type of `a' and the identifier `b' represents. [1]
There is another example of where verbosity is useful. In the English
language, the information entropy is roughly ~1-1.5 bits/letter, so we
actually only theoretically need about 3 letters to keep text on average
the same size. Why don't we? Bcs it enbles us to rd txt whn som letrs r
mssng or malfermed.
[1] I do realize that a.b is already ambiguous syntax, thanks to Java
overloading the meaning of the `.' to be both a scope resolution
operator (e.g., java.io.Serializable) and a member access operator. In
this case, I would classify nested classes as falling under `scope
resolution' (so if we had used '::' for scope resolution, Map.Entry
would be `Map::Entry'). The decision to overload `.' is not one I'm
particularly thrilled with.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth