Notify developer when class is at max of its parents

This is related to squid:MaximumInheritanceDepth, which seems reasonable.

I’m creating a library which is supposed to be extendable. Some classes may be open to modifications, that’s also why the default visibility in this project is protected.

But there might be situation where I don’t notice that class I’m designing for extension, already reached max of its parents (default: 5).

Then the client, to extend it, would need to silence/change the rule.

Do you think it would be good idea to suggest developer to make such a class final? It would be consistent with the above rule.

Or there is already some mechanism for such situations?
Or is it really flawed design and I’m missing something here?

Hi Ewa,

Thank you for this interesting question.

The rule squid:MaximumInheritanceDepth is a general guideline which warns developers when the architecture looks difficult to maintain. It is configurable because designs vary a lot from one project to the next. JDK 9 for example has classes which go to 8 levels of inheritance.

As this is an architectural choice, there are different solutions. Developers seeing this issue could decide that they will use composition instead of inheritance. Making a class final shouldn’t be decided because of the inheritance depth, but only when it doesn’t make sense to extend this class. Thus a rule forcing classes to be final because of their inheritance depth would be confusing. In this case we can warn the developer but I don’t think that we can chose a solution for her/him.

Does it answer your question?

Cheers,
Nicolas