Design and Architecture
Abstractness vs Instability
A chart that tells, for each part of your code, whether its stability matches its level of abstraction. Stable code that cannot be extended becomes painful to change. Abstract code that nobody uses is useless. The chart shows both at once.

What it shows
Each point is a unit of code (a project or a namespace, depending on the level you choose). The horizontal position is its abstractness, the vertical position is its instability. A dashed line, the main sequence, joins the two ideal corners. The closer a point is to this line, the better balanced it is. The metrics are the ones defined by Robert C. Martin.
The metrics
Instability (I)
I = Ce / (Ca + Ce)
- Ca (afferent coupling): how many other units depend on this one.
- Ce (efferent coupling): how many units this one depends on.
I goes from 0 to 1. 0: many depend on it and it depends on nothing, it is very stable and costly to change. 1: it depends on others and nobody depends on it, it is unstable and cheap to change. Neither is bad by itself: a top-level application should be unstable, a core library should be stable.
Abstractness (A)
A = abstract types / all types
A goes from 0 (only concrete types) to 1 (only abstract types). An abstract type is one that cannot be instantiated and exists to be implemented or extended: an interface, an abstract class.
Distance from the main sequence (D)
D = | A + I - 1 |
D goes from 0 (on the line) to 1 (in a far corner). It is a single number to sort by: the highest D are the units to look at first.
| Unit | A | I | D | Verdict |
|---|---|---|---|---|
Application, main | 0.0 | 1.0 | 0.0 | Fine: concrete and unstable |
| Interface-only library | 1.0 | 0.0 | 0.0 | Fine: abstract and stable |
| Core utilities, all concrete, used everywhere | 0.05 | 0.1 | 0.85 | Zone of pain |
| Interfaces that nobody uses | 0.9 | 0.95 | 0.85 | Zone of uselessness |
Reading the chart
The two zones
| Zone | Where | Why it hurts | Typical case |
|---|---|---|---|
| Zone of pain | Bottom left: stable and concrete | Many depend on it, so every change is risky, and there is no abstraction to change it behind. The code is rigid. | A shared utilities library, a data layer used directly everywhere. |
| Zone of uselessness | Top right: unstable and abstract | Abstractions that nobody uses. They cost maintenance and give nothing. | An interface layer designed "for later", abandoned classes. |
In C and C++
- Abstract type: a class with at least one pure virtual function (
= 0). Templates, concepts and function pointers also create abstraction in C++, but they are not counted as abstract types, so a template-heavy library can look more concrete than it is. - Header-only libraries: their types are usually concrete templates. A low A there is expected and does not mean poor design.
- C code: there are no abstract classes. Units written in C will always have A = 0, and only the instability tells something. Read D with that in mind.
- The unit: choose namespaces for a code base that uses them, or projects for a code base with one library per folder. Code in the global namespace is a single unit, which hides its structure.
- Third party and generated code: exclude them (see the project excludes), otherwise they dominate the zone of pain.
What to do
A unit is in the zone of pain
- Find what changes most in it (history, or the issues view).
- Put an abstract interface in front of the part that changes, and make the users depend on the interface.
- Run the analysis again: A goes up, and the dependents now depend on an abstraction.
A unit is in the zone of uselessness
- Check who uses its abstract types (Ca is low).
- Remove the abstractions nobody implements or uses, or move them next to their only user.
A unit is far from the line but fine
Use the exceptions of your rules (for example for generated code), and keep the rest of the rule active. Do not disable the rule for the whole project.
Rules (CQLinq)
The same metrics are available in CQLinq, so you can fail a quality gate when a unit is too far from the main sequence.
// Namespaces far from the main sequence
warnif count > 0
from n in Application.Namespaces
let a = n.Types.Count(t => t.IsAbstract) / (double) System.Math.Max(1, n.Types.Count())
let ce = n.NamespacesUsed.Count()
let ca = n.NamespacesUsingMe.Count()
let i = ce / (double) System.Math.Max(1, ca + ce)
let d = System.Math.Abs(a + i - 1)
where d > 0.7 && n.Types.Count() >= 5
orderby d descending
select new { n, A = a, I = i, Distance = d, ca, ce }
Limits
- The metrics measure structure, not quality: a unit on the main sequence can still be badly designed.
- Coupling is counted between units, not between types: a unit with one heavily used class and many unused ones has the same Ca as one where all are used equally.
- The result depends on the unit you choose and on what you exclude.
- Use it to find where to look, and combine it with the dependency cycles and the Architecture Blueprint.
Questions
Everything is at A = 0. Is it a bug?
No. A project without abstract classes (pure virtual functions) has A = 0 everywhere. This is usual for C code and for code built on templates. Only the instability and the cycles are useful there.
Which value of D is too high?
As a rule of thumb, look above 0.7 and ignore very small units. Compare the trend between two analyses rather than the absolute value.
The utilities library is in the zone of pain. Must I change it?
Only if it changes often. A stable concrete library that is rarely modified is not a problem in practice.
What is the difference with the dependency cycles rule?
Cycles say who depends on whom in a loop. This chart says whether a unit is too rigid or too abstract for its position. Both are needed.
Thanks for your feedback. Tell us more or request a feature.
