March 02, 2004

POGO's

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.

Posted by cedric at March 2, 2004 06:16 AM
Comments

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?

Posted by: Christian at March 2, 2004 07:58 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.

Posted by: Cedric at March 2, 2004 09:15 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.

Posted by: Dan Weinreb at March 3, 2004 12:57 AM

Jython gives you properties for Java objects too:

http://www.jython.org/docs/properties.html

Posted by: Philip Aston at March 6, 2004 02:23 AM
Post a comment






Remember personal info?