Should I seal all classes I know shouldn't ever be used as a base class?

C#Coding StyleClassSealed

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.)

Solution 3 - C#

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.

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel CoffmanView Question on Stackoverflow
Solution 1 - C#Eric LippertView Answer on Stackoverflow
Solution 2 - C#Paul SasikView Answer on Stackoverflow
Solution 3 - C#Brian RasmussenView Answer on Stackoverflow
Solution 4 - C#NotMeView Answer on Stackoverflow