C# · CodeProject · Dotnet

Reason to make your class Sealed by default

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 🙂


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s