Skip to main content
1 of 3

How to calculate number of indirect dependencies of a class?

Most of the static code analysis tools which analyse class dependencies generate dependency pairs of classes where each pair represents a direct dependency between two classes. Given those dependency pairs, it is straight forward to calculate number of direct dependencies of a particular class. However, if we have to compute number of indirect dependencies of a class, I realized that the algorithm is fairly complex. Let me clarify what I mean by indirect dependency:

We have following direct dependency class pairs:

Class A depends on Class B

Class B depends on Class C

Class B depends on Class D

In this case, class A depends directly on class B and indirectly on classes C and D.

I would define number of direct dependencies for class A as 1 and number of indirect dependencies for class A as 2.

I would like to compute indirect dependencies for each and every class in the systems that I analyse.

The complexity arises mainly due to cyclic dependencies.

For example, if we were to compute number of indirect dependencies for class A:

Scenario 1:

A -> B

B -> C

C -> D

D -> B

Scenario 2:

A -> B

B -> C

B -> D

C -> E

D -> F

E -> G

F -> G

G -> H

Scenario 3:

A -> B

B -> C

C -> A

There are two possibilities to get this:

  1. Is there a tool/library that can compute this? It could be a static analysis tool or something that is totally different but accepts above kind of dependency data and calculates what I am looking for.

  2. I can represent the direct dependency pairs as a directed graph (graph theory) and may be graph theory has a solution to find this out. Something like, recursive sum of outdegrees of adjacent nodes.

Let me know what is the best way to get it.