Sunday, October 18, 2009

Inheritance depth

I’m developing an application and I’m controlling code quality with NDepend. There is a CQL rule for checking inheritance depth. The built in rule is the following:

WARN IF Count > 0 IN 
SELECT TOP 10 TYPES
WHERE DepthOfInheritance >= 6
ORDER BY DepthOfInheritance DESC

The project is a WPF-based application, so I got a lot of warning. I have controls and windows, all of them starting at inheritance level 9. So I rewrote the CQL query as follows:

WARN IF Count > 0 IN 
SELECT TOP 10 TYPES
WHERE
(
(DepthOfInheritance >= 6 AND
! DeriveFrom "System.Windows.Controls.UserControl" AND
! DeriveFrom "System.Windows.Window" )
)
OR ( DepthOfInheritance >= 12 )
ORDER BY DepthOfInheritance DESC

This rule will warn me if

  • the component  is not derived from UserControl or Window and the inheritance level is higher than 6 or
  • the inheritance level is higher than 12.

In other words I allow 3 inheritance level for user controls and windows and 6 for others.