# Tuesday, 27 January 2009

http://www.developer.com/img/2002/09/18/OOP01.gifToday I read a nice post by Brian Rasmussen in which he describes how to set up Visual Studio to generate class definitions which are sealed by default. I had to post my own point of view in the matter although it is going to be awkward. Not in the teenage, “define me”-sense but in my choice of language as I can’t really quote him effectively, so you’ll have to make do with me paraphrasing his post :)

Now I’d like to put myself in the I-could-not-disagree-more camp. The default choice in my humble opinion should be to leave classes open and have all members be virtual if you want to take it to the extreme. This would leave the system open for change just as the SOLID principles state. Java got it right in my opinion.

To be able to make the decision on whether a class should be open for inheritance you’d have to travel to the future to see what the class might be used for. If you’re anything like me you’re probably challenged in the time travelling department, and so I postulate that you can’t really make a good decision in the matter. More often than not closing the system for change will be the wrong choice as requirements and environments change.

I do agree with Brian’s statement that sealing a class would take away options thus creating a simpler API. I would, however, also like to state that there are better ways of achieving a simple API. How about not exposing the type all? Why not create a simple interface, which exposes only what is needed for the task at hand?

Please don’t make the default choice for your classes sealed. Go with open classes and live a happy life with a system, which is open for change. Trust me I’ve seen systems, which adopted a closed stance and it wasn’t pretty. The team kept hitting the wall in the changes they wanted to make, simply due to the fact that the original developer had no time machine, which enabled him to foresee the changes, which future members of the team needed to implement.

Comments are closed.