Thursday, April 30, 2009

The dictatorship of the designer

This is about why I hate exceptions and the private, protected, final, virtual keywords.

Obtaining reusability is a lot about creating API’s or code with great flexibility. If your design was great, your users will use it in ways you never imagined. But often the designer "knows what is best for you", and his or her design becomes inflexible.

The lauded information hiding principle is abused, so stuff not only are hidden (hidden stuff can usually be found) but generally unavailable because its private protected, final or not declared virtual. The sentiment behind these keywords is fine, but I would prefer they were hints to the compiler - so it could warn me if I violated them - BUT THAT I COULD VIOLATE THEM! Actually they should not even be hints, since they are a matter of convention, and conventions IMO should be tested for (unit testing), not built into the language (not unless testing is built in at least). Dawn does not have these keywords, although it will be possible to extend Dawn with them (but since such extensions are entirely local, I would be quite happy if someone made that extension - when Dawn has been published)

Exceptions are another side of this curse. They are not something you can choose to use. Unchecked or checked, they p*ss me off. Unchecked, because they in effect becomes c style null pointer exception bringing my program down, Checked because I have to rethrow them (at least often). Dealing with these API's, the first thing on my list is usually a wrapper that handles the exceptions, but gracefully encapsulates usage - maybe it does nothing, but it’s quiet. Exceptions are like Microsoft Dialog boxes, typically in your face and annoying and you can’t turn them off.

The last design issue is about types. The other day it dawned on me that types really just are a convention. Since Dawn is prototype based, it does not have types, but utilizes "duck typing" (if it walks like a duck and quacks like a duck then it is a duck), which in other words means that the interface to an object defines its type. And the interface is the convention. As stated earlier I want conventions tested, not part of the language itself.

Before I get flamed for all this, I must say that these ideas/realizations are somewhat intimidating to me, because my entire career has been spent in programming type strong languages. Dawn comes from a few basic thoughts about reuse, and I’m trying to follow them consequently to see where it leads me.

Labels: ,

2 Comments:

Anonymous Anonymous said...

I am not the biggest fan of exceptions either. I think you should be allowed to use exceptions only after having programmed for X years without them (where x > 9)

But banning private/protected/public keywords? BURN THE HERETIC!

:-)
Antonello

May 4, 2009 1:32 PM  
Blogger phook said...

Not banning - making them into hints, so the compiler or testframework can warn you if you violate them - point being of course that you CAN violate them. I just yesterday have to create a wrapper class for a class call a protected method from the outside.

Actually I think exceptions should be optional, so if you specify that you catch them, then they get thrown. Otherwise the API should just handle them gracefully.

May 5, 2009 12:45 AM  

Post a Comment

<< Home