I've just watched the
Danny Corward presentation at the Prague JUG on what could be coming in the JSE 7.0. In that presentation he demonstrated the "->" operator. The purpose of the operator is to provide a short hand notation for properties. The syntax that we would normally use is;
a.setFoo( b.getFoo());
With the "->" operator we would see;
a->foo = b->foo;
How we'd currrently coding this is;
a.foo = b.foo where foo would have to be more visiable than private.
Data encapsulation has long been proven to be a useful design and coding abstraction. So much so that the evolutionary pattern of language design is more support for abstractions that support this important concept. The most significant advance was support for the encapsulation of both state and behavior (otherwise known as objects).
Objects are a natural evolution from data structures. In fact, the first C++ implementations were more about translating object like syntax into C syntax using CFront (C with classes) compiler. The biggest advantage of moving to objects from data structures is the further separation of representation from implementation. Separation of representation from implementation is a notion that has now found prominence in API design guidelines. This idea is supported by slogans such as "code to the interface, not the implementation".
Encapsulation offers many advantages including support of the DRY principle. As stated by Dave Thomas in an interview with
Bill Venners;
"DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation."
In my opinion the most important idea in this quote is the phrase, "one authoritative unambiguous representation". The fall-out from this from a coding perspective is that not only is the representation encapsulated, the logic for managing the state is also encapsulated. If we (as developers) allow state to be exported we have lost our ability to maintain consistency in our internal state. We are depending upon the client to do the right thing and more importantly, if conditions change, the client to play along with the new rules of the game. The bigger the system, the greater the chance that the client won’t.
So if you’ve made it this far you maybe wondering what the hell does this have to do with the new language feature? Lets review the code.
a->foo = b->foo;
I don’t know what foo is nor do I really care but "a" better care and more importantly it should have a fighting chance to care. Using the operator ( or the "." on a (semi) public variable) means that "a" doesn’t even have a fighting change of caring. The integrity of the object has been totally compromised by a "cool new" syntax that encourages violations of encapsulation.
If this syntax can on encourage behavior that we don’t want than why on earth would the authors of the JSE 7.0 JSR propose such a change? My real answer is; since I’ve not spoken to the authors I don’t really know. However the evidence is the use case is ease of use for Java beans. Simply put, the spec lead for the next version of Java would like to change the language to handle an edge case while ignoring the potential of creating havoc in just about every other language use case.
So to answer Danny Cowards question, wouldn’t it be nicer if we could do something like to do something life a->foo = b->foo;. My answer is no. I think that Danny shares that view though we may want to make sure!
tags: java jse 70 beans