Knots is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.
Knots
API ID: KnotsLanguages: Ada, C#, C++, Java, Rust
Targets: Functions, Packages, Subprograms
Measure of overlapping jumps.
If a piece of code has arrowed lines indicating where every jump in the flow of control occurs, a knot is defined as where two such lines cross each other. The number of knots is proportional to the complexity of the control flow.
For example:
void knotsDemo() {
while (1) {
if (a)
break;
if (b || c) {
if (d || e) {
}
else {
if (i)
dosomething();
else if (j)
dosomething();
else if (k)
dosomething();
else {}
}
}
}
}
Each if, while, and break is a jump: draw an arrow from each one to every line it can transfer control to, and count how many pairs of those arrows cross. For this function, that's 6 knots.
Unlike Essential Complexity, which measures unstructured control flow by reducing the graph and taking the complexity of what's left, Knots measures it directly by counting crossings in the raw (unreduced) control-flow graph. See Max and Min Essential Knots for the knot count within the reduced graph.
Introduced by Woodward, M.R., M.A. Hennell, and D. Hedley in "A Measure of Control Flow Complexity in Program Text." IEEE Transactions on Software Engineering, vol. SE-5, no. 1, 1979, pp. 45-50.
- Ada: Type, Entry, Function, Package, Procedure, Protected, Task
- C#: Method
- C++: Function
- Java: Method
- Rust: Function
- This metric can be enabled from "Project Configuration/Metrics/Complexity" with the "Knots" option.