Click here to Skip to main content
15,885,875 members
Articles / All Topics

Reason to make your class Sealed by default

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Nov 2011LGPL33 min read 17.4K   4
Dear Reader, Lately i have been reviewing/refactoring alot of code, and i must say many a times some code pushed me to a confused state, because they are not sealed. Now reading this you may wonder that i might sound silly. The problem is with inheritance here. When there are alot of code in your [.

Dear Reader,

Lately i have been reviewing/refactoring alot of code, and i must say many a times some code pushed me to a confused state, because they are not sealed. Now reading this you may wonder that i might sound silly. The problem is with inheritance here.

When there are alot of code in your repository which your refactoring/reviewing for what ever reasons and in my case there are parallel repositories of code which we and our offshore counterpart work on. The code i am working is a common code which many other projects uses. So often reviewing and fixing some warnings or even refactoring i come to a state whether the current class i am working on is having subclasses or not.

One such reason i find it strong for having default sealed class is for implementing IDispose pattern. There are 2 different patterns available actually, one for normal sealed classes where in you don’t make your private Dispose(bool) as virtual and protected and other pattern where you make Dispose(bool) method protected and virtual to make it available for all its sub-classes.

Allow me to explain my scenario via a code:

class CustomClass{private Process _someProcess;public void SomeMethod(){//Does some work with the process
}}

As you can see from the above code, every thing looks fine and even you do not get any compiler error/warning if you build this code. But you need to keep in mind that, if you are using a disposable member then its a best practice to implement IDisposable interface and call this disposable member’s Dispose() method. Such a way you’re not playing with any memory related issues.

So the same thing is happening to me from last 2 days, where in i have to go through many classes (so far 10 i guess) which are not sealed and does not implement dispose pattern. So since you already know that there is 2 styles of dispose pattern, i set out to implement one of them.

But there comes the problem which one i should implement? Yes you may say that go for inheritance one because my class will be as per O/C principle, yes even i thought the same at first. But upon analyzing the code i did not find any members having protected modifier and neither virtual methods. So this was first hint for me to confirm that yes i can make it a sealed class.

But as said my core code is being used by many different projects across repositories for which i do not even have any access. So i had to make couple of contacts and talk to few people in seeing if this class is having sub-classes (may be a crazy developer might have implemented it?) so it’s not good for me to just change and wait for a nightly build to fail because of this.

Thus, it almost took 15-20 minutes for me to fix this issue after consulting many folks and other things. Since my code base is huge so it’s a pain for me to face this issue every time. That is why, you as a developer make a habit of making every class as sealed even though i did wish visual studio did this by default. This way you could save your future time or some body else.

By doing this at first step, you are sure not going to break any code in any code base, in the future if you decide to make it non-sealed then you can easily with out a worry can change it and still will not break any code. But if you want to do the other way, then you are in a big mess my friend.

Hope it helps. Your comments are welcome.

Happy coding, thanks :)

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
QuestionThat't true on the override part Pin
VallarasuS20-Nov-11 8:07
VallarasuS20-Nov-11 8:07 
AnswerRe: That't true on the override part Pin
zenwalker198520-Nov-11 15:13
zenwalker198520-Nov-11 15:13 
QuestionExample code has error Pin
MacSpudster15-Nov-11 12:00
professionalMacSpudster15-Nov-11 12:00 
AnswerRe: Example code has error Pin
zenwalker198515-Nov-11 16:02
zenwalker198515-Nov-11 16:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.