As I confessed earlier,
I am a big fan of properties, whether they are implemented in C# or Ruby.
Not a day goes by without me missing them thoroughly in Java land, as I
tirelessly write an endless stream of "int m_foo; public int getFoo() { …};
public void setFoo(int n) { …}" (actually, my IDE does that for me but it’s
still a waste of space and it gets in the way of readability).
How come I am not surprised that James found such a neat way to
solve this conundrum in Groovy?
James call them POGO’s (Plain Old Groovy Objects), and declaring them is as
simple as declaring your fields public. The Groovy compiler takes care of
generating the boiler plate accessors without them getting in the way of your
published interface. I think it’s important to be able to control the
access to your fields, but I also believe it’s very important for this access
code not to obfuscate your code more than necessary.
Once again, Groovy hits the perfect sweet spot between Ruby and C#.
Well done, James.
#1 by Christian on March 2, 2004 - 7:58 am
I can certainly feel your pain when the accessors/mutators for a property are just your regular companions of
Object getXXX() { return xxx; }
void setXXX(Object newValue) { xxx = newValue; }
however, what should happen when you actually have to implement some logic in your mutators, especially when thinking about a GUI where a mutator usually fires a PropertyChangeEvent to notify its listener of the new values:
void setXXX(Object newValue) {
Object oldValue = getXXX();
xxx = newValue;
firePropertyChange(PROPERTY, oldValue, newValue);
}
it gets worse from here, when your mutator is actually just a convenience method of changing a specified value from an underlying Collection of values …
any thoughts?
#2 by Cedric on March 2, 2004 - 9:15 am
I always have thoughts 🙂
Groovy, Ruby and C# definitely tackle this case. You implement your accessors the same way we do it in Java today, but you can still access them through the simpler syntax.
Best of all worlds.
#3 by Dan Weinreb on March 3, 2004 - 12:57 am
Yes, it’s a good idea. It was a good idea when we did it twenty-five years ago in Flavors, too. See
Lisp Machine Manual, D.L. Weinreb and D.A. Moon, Artificial Intelligence Laboratory, MIT (Cambridge, MA), 1980.
#4 by Philip Aston on March 6, 2004 - 2:23 am
Jython gives you properties for Java objects too:
http://www.jython.org/docs/properties.html