56

Should I seal all classes I know shouldn't ever be used as a base class even when there are no tangible performance or security concerns, or is this just adding cruft?

Daniel Coffman
  • 1,997
  • 3
  • 26
  • 34
  • [Somone said, and I read:](http://programmers.stackexchange.com/a/210481/4261) don't *ever* seal a class unless you *know* you'll have support issues with your clients. – cregox Sep 04 '13 at 19:29

4 Answers4

57

A class which is extensible implements the feature that it can be extended -- that's a feature like any other feature of the class, and should be treated like one, no different from a method. All features should be thought through carefully to ensure that they meet the goals of the customer using the feature. Features need to be designed, implemented, reviewed for security problems, debugged, documented and maintained.

All that costs effort, and effort usually requires the outlay of money. Whose money are you spending? They might have an opinion on whether you should do this feature or not.

Basically, you have three choices:

1) Spend the money to do the feature so that you have confidence that it is correct, robust, secure and meets user needs.

2) Do none of the above but ship the feature anyway and hope that shipping an undesigned, rapidly implemented, untested, undocumented, unmaintained feature with unknown security risks doesn't harm you, your employer or your customers.

3) Seal the class. Unseal it later if you find that (1) was the right choice.

I say that (3) is good value for the money. I always seal every class I write that was not designed for extensibility.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 4
    Eric, Here's a question for you. Just like how you have to declare a method 'virtual' before you can override it, why did C# not adopt something similar to indicate that a class can be inherited and seal it by default. Just curious. – SolutionYogi Jan 29 '10 at 22:11
  • I'll buy this. Do you believe C# classes should be sealed by default? – Daniel Coffman Jan 29 '10 at 22:18
  • 22
    @Daniel, @SolutionYogi: I wish we had done so. However, I am actually in a minority; there are a great many people who believe that you should only seal classes when you have a reason to do so. I disagree; I think you should only unseal classes when you have a reason to do so. – Eric Lippert Jan 29 '10 at 22:32
  • Also, I agree with Atwood; why purple text? – Daniel Coffman Jan 29 '10 at 22:33
  • 4
    @Daniel: Because I think it's awesome. (If you read the blog with an RSS reader then you'll get it unformatted.) – Eric Lippert Jan 29 '10 at 23:43
  • It seems there are deleted comments. What blog post are you guys referring to? And what did Atwood said? – Sunny Milenov Sep 05 '13 at 14:04
  • 3
    @SunnyMilenov: The blog post of mine they're referring to is: http://blogs.msdn.com/b/ericlippert/archive/2004/01/22/61803.aspx. The blog post of Jeff Atwood's referred to is: http://www.codinghorror.com/blog/2006/12/eric-lipperts-purple-crayon.html. My response to Jeff is here: http://blogs.msdn.com/b/ericlippert/archive/2007/01/08/the-horror-the-horror.aspx. Tragically, the response is no longer in purple lucida. Isn't it ironic, don't you think? – Eric Lippert Sep 05 '13 at 15:14
14

Setting a class to be sealed is not cruft since doing so sets a strict rule in your code: This class cannot be inherited.

Code is only cruft if it is unnecessary and confusing.

That said, one school of thought (and simple rule-of-thumb) is that you should always seal all classes since it's easy to unseal them if necessary but not vice-versa. Some code generators do this automatically. (See Eric Lippert's option #3 above. It basically says the same thing.)

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 1
    Like almost every answer on how to do things, it depends. There are some optimizations that can be gained by sealing classes by default and only unsealing when needed. Take a look at: http://msdn.microsoft.com/en-us/library/ms998547.aspx http://dotnetperls.com/sealed-1 – Firestrand Jan 29 '10 at 19:07
7

I wouldn't consider it to be adding cruft at all. Instead you're clearly expressing your intentions for the class.

Classes should be designed for inheritance or be sealed. Unfortunately, classes are not sealed per default in C# so you have to include the keyword yourself. Personally, I would have preferred a keyword to explicitly make classes available for inheritance since that would prevent people from using a class as a base class unless it was explicitly marked as such.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
5

Yes. If nothing else it's a signpost letting others know that they shouldn't go further down the trail.

NotMe
  • 87,343
  • 27
  • 171
  • 245