Should I recommend sealing classes by default?

后端 未结 13 998
青春惊慌失措
青春惊慌失措 2021-01-30 01:57

In a big project I work for, I am considering recommending other programmers to always seal their classes if they haven\'t considered how their classes should be subclassed. Oft

13条回答
  •  粉色の甜心
    2021-01-30 02:43

    It is my opinion that architectural design decisions are made to communicate to other developers (including future maintenance developers) something important.

    Sealing classes communicates that the implementation should not be overridden. It communicates that the class should not be impersonated. There are good reasons to seal.

    If you take the unusual approach of sealing everything (and this is unusual), then your design decisions now communicate things that are really not important - like that the class wasn't intended to be inherited by the original/authoring developer.

    But then how would you communicate to other developers that the class should not be inherited because of something? You really can't. You are stuck.

    Also, sealing a class doesn't improve readability. I just don't see that. If inheritance is a problem in OOP development, then we have a much larger problem.

提交回复
热议问题