It took 34 days since the initial posting (and my reporting), but it seems that some of the Java language designers and other Sun folks (Ah��, Buckley, Muller) are moving away from the syntax for properties.
I believe Hans Muller's post asked the most fundamental question: What are properties for?
And if I remember correctly, the properties concept came into Java through the JavaBeans specification in JDK 1.1. It was supposed to be an abstraction layer of the underlying mechanisms for GUI builders, like Microsoft's COM. Properties, together with the concept of methods and events form the foundation of a GUI component system. And of particular utility are a couple of combinations of properties and events: the constrained properties and vetoable properties.
So properties, in that context, does something useful. For example, when you set the text property on a Label, it will have go find the appropriate glyphs from its font and erase the old text and paint the new text.
Somewhere along the line, the concept of properties were subverted. It became a mindless chant:
// Exhibit A
public int foo;
is not OO! Change it to
// Exhibit B
private int foo;
public void setFoo(int value) {
this.foo = foo;
}
public int getFoo() {
return foo;
}
to make it OO. And along the way, we, with our favorite IDEs as accomplices, managed to bloat the world total Java code base by about 10% (maybe more).
It is these IDE-generated, boilerplate, pointless bloat that most people hate. And we wish we can do
a.foo = b.foo;
instead of
a.setFoo(b.getFoo());
Well, there is an obvious solution to this (minor) gripe. Can you guess what it is? You're right. You can accomplish this goal without addition to the Java programming language. You can simply turn all instances of Exhibit B into Exhibit A. There should be a refactoring called "Remove frivolous getters and setters" that does it automatically.
Go ahead and do it. Should take about ten minutes.
You done? Did it break your app? Probably not.
Do you have any getters and setters left? If not, you're done. And you can go home now.
The remaining getters and setters are the true properties. Now stare at them for a minute. (Here's an example from the JDK:
public void setNextFocusableComponent(Component aComponent) {
boolean displayable = isDisplayable();
if (displayable) {
deregisterNextFocusableComponent();
}
putClientProperty(NEXT_FOCUS, aComponent);
if (displayable) {
registerNextFocusableComponent(aComponent);
}
}
from javax.swing.JComponent.)
Still feel like some syntactic sugar for properties? Go ahead and add some. (Here's a tip: the Microsoft folks have property support in C# for six years. I wouldn't mind that syntax!)