Wednesday, February 27, 2008

Open Classes in Ruby - Too easy to be misused ?

I read this ..
Open classes makes invention possible in real time. No committee. No waiting for the next language revision. If you think of it, you can try it. Does Symbol#to_proc make it worthwhile? How about andand? Why don’t you tell me? I was serious when I asked you what you think of it.

and soon after .. this ..

I am all with Raganwald that "dangerous features enable bottom-up language evolution". My main concern, however, is that, I have seen too much of Ruby code where people tend to open up classes without even considering other design choices. On many of these occasions did I feel that mixins could have been a more pragmatic alternative. Maybe, one reason could be that, open classes in Ruby are too easy to implement. And developers give in to the temptation of using this "dangerous feature", without giving the adequate amount of thought that the design decision deserves. Scala's lexically scoped alternative is less sexy, but looks more pragmatic.

2 comments:

Reginald Braithwaite said...

So... do we need languages where the emphasis is on preventing inexperienced programmers from shooting themselves in the foot?

Debasish said...

Hi Reg -

I am not trying to put forward a strawman's argument. I surely believe in dangerous features offering more productivity and power to *good* programmers. What I narrated regarding open classes in Ruby is pure experience. Open classes are dangerously powerful, which I like, but, in Ruby they operate on the global scope, which I don't like. At the same time it is too easy to open up a class and start writing methods, without adequate considerations for alternatives. As I mentioned in the blog, in many cases, the problem could have been better addressed using mixins, which is a far less dangerous and more pragmatic way of doing the same thing. This may seem to be supporting for the inexperienced programmer, but this is my personal experience.

And thanks for stopping by.