Monday 11 August 2008

Today's Object-Oriented Productivity Hint

Please try using encapsulation before attempting metaprogramming or functional programming.

Exercise:

Find three reasons not to write this kind of code:

myObject."$localVariable".attribute = 42

This kind of code is wrong on so many levels that if Ali G wrote web apps, he'd love it.


1 comment:

Julien said...

I don't know. Is it because 42 is the answer to life ;)

I agree this snipet have a smell. Depending on the context there might be different way of refactoring this type of code, and sometime this could be a legitimate use of the dynamic facet of Groovy.

The bigger problem here is the use of a getter (getLocalVariable()) and everyone knows that getter and setter are evil. A different approach would be to ask the object to do something with 42.