Understanding your Project with metrics: Inheritance Metrics DIT and NOC

The software architecture metrics “Depth of Inheritance Tree” (DIT) and “Number of Children” (NOC) provide insights into inheritance dependencies between classes.

Depth of Inheritance Tree (DIT) measures the complexity of a class hierarchy. It represents the number of levels from a given class up to the root class in the inheritance hierarchy, the deeper the hierarchy, the higher the DIT value.

Given two classes, class A and class B, with class A being the root class that doesn’t inherit from another class and B being a subclass of A. Then A has a DIT of 1 and B has a DIT of 2. A high DIT value indicates deep inheritance, which can promote code reuse but may also increase complexity. 

The starting point for DIT can vary:

It’s important to clarify which convention is being used when interpreting DIT in different tools and frameworks. Sw!ftalyzer as well as the following examples in this post start at 1.

Number of Children (NOC) measures the width of an inheritance hierarchy level. It represents the number of subclasses that inherit from a common super class.

Looking at the example from above with class A and class B, A has a NOC of 1 and B has a NOC of 0. The code of a class with a high NOC value needs to be changed carefully, because it may be used or overridden by many subclasses.

Calculating DIT and NOC


The first step to calculating DIT is to identify all inheritance dependencies in your project. Afterwards you can count the inheritance hierarchy levels for each class, the formulas is DIT(class) = DIT(parent) + 1.

Based on the inheritance dependencies you can also calculate NOC by counting all classes that inherit from a given class.

Take a look at an example. Here are 5 code entities, 4 classes and 1 protocol.

class Class0 { }
protocol SomeProtocol { }
class Class1: SomeProtocol { }
class Class2: Class1 { }
class Class3: Class2 { }
class Class4: Class2 { }


This is the dependency graph for the example code above:

Dependency graph for code example.

There are two dependency hierarchies, the first one only consisting of the class Class0 and the other one starting with the protocol SomeProtocol and ending with Class3 and Class4.

Class0 is a base class and has a DIT value of 1. It has no children, thus its NOC value is 0.

SomeProtocol is not a class and thus has neither a DIT nor a NOC value.

Class1 conforms to SomeProtocol, but as this is no inheritance, its DIT value is 1. It has one child, so it has a NOC of 1.

Class2 is a subclass of Class1 and thus has a DIT value of 2. Both Class3 and Class4 inherit from it, so its NOC value is 2.

Class3 inherits from Class2, so it has a DIT value of 3. Without any inheriting class, it has a NOC value of 0. Same holds for Class4.

The following table summarizes the DIT and NOC values:

NameComponent TypeDepth of Inheritance Tree (DIT)Number of Children (NOC)
Class0class10
Some Protocolprotocol
Class1class11
Class2class22
Class3class30
Class4class30
Values for example graph.

Analyzing DIT and NOC with Sw!ftalzyer


Starting with version 1.4.0 Sw!ftalyzer automatically calculates and shows DIT and NOC values for your project.

Sw!ftalyzer only inspects the entities in your project and in internal packages. Classes in Apples frameworks or external frameworks embedded via SPM or CocoaPods are not included. This means that for example your ViewControllers that inherit from UIViewController start with a DIT value of 1.

As with other metrics, Sw!ftalyzer shows you this information in two different ways, details about the selected node in the 1st tab of the information panel and an overview of the whole project in the 3rd tab of the information panel.

The first one looks like the screenshot on the left. For a given class you find its DIT value followed by a list of parent classes where the top one is the direct parent class and the bottom one is the root of the inheritance hierarchy. This enables you to easily find your position in the hierarchy tree. A right click on the dark gray box representing a node opens a context menu. With this you can focus the node and thus quickly navigate to related classes. It also shows you its NOC value with a list of children.

The screenshot on the right shows the project wide overview. You see the number of inheritance trees in the project as well as the depth of the deepest ones. These are shown underneath, in this example Class3 and Class4 are the classs with the highest DIT value. As before, you can navigate to a node via the context menu opened by a right click. This provides a convenient access to the nodes with the highest DIT values for further inspection.

Additionally, you can find these values and many more in the “Metrics & Statistics” csv file. You can export this file via the export menu shown in the last screenshot below:

Annotated screenshot of Swiftalyzer showing how to export the Metrics & Statistics csv file. The first annotation highlights the export button in the header that opens the export menu. The second annotation highlights the third button in the export menu.
Exporting the “Metrics & Statistics” csv file.