Over a million developers have joined DZone.

Abstractness vs. Instability: Neo4j Case Study

DZone's Guide to

Abstractness vs. Instability: Neo4j Case Study

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

Robert C.Martin wrote an interesting article about a set of metrics that can be used to measure the quality of an object-oriented design in terms of the interdependence between the subsystems of that design.

Here’s from the article what he said about the interdependence between modules:

What is it that makes a design rigid, fragile and difficult to reuse. It is the interdependence of the subsystems within that design. A design is rigid if it cannot be easily changed. Such rigidity is due to the fact that a single change to heavily interdependent software begins a cascade of changes in dependent modules. When the extent of that cascade of change cannot be predicted by the designers or maintainers the impact of the change cannot be estimated. This makes the cost of the change impossible to estimate. Managers, faced with such unpredictability, become reluctant to authorize changes. Thus the design becomes rigid.

And to fight the rigidity he introduce metrics like Afferent coupling, Efferent coupling, Abstractness and Instability.

Let’s discover all these metrics and how they could be very useful to improve the design of applications. For that let’s analyze Neo4j by JArchitect.

Neo4j is a robust transactional property graph database. Due to its graph data model, Neo4j is highly agile and blazing fast. For connected data operations, Neo4j runs a thousand times faster than relational databases.

And here’s the dependency graph between all Neo4j jars


Neo4j contains many jars and all of them depend on neo4j-kernel, and to have more details about the weight of using each jar, The DSM (Dependency Structure Matrix) is a compact way to represent and navigate across dependencies between components.


As the matrix shows the neo4j kernel is heavily used by the other jars.

Afferent Coupling:
The number of types outside this project that depend on types within this project.

Let’s execute the following CQLinq query to get the afferent coupling of Neo4j jars:

    from p in Projects where !p.IsThirdParty select new { p,p.NbTypesUsingMe }


As discovered before the kernel is the more solicited by the other jars.

Efferent Coupling
The number of types outside this project used by types of this project.

    from p in Projects where !p.IsThirdParty select new { p,p.NbTypesUsed }


The efferent coupling and afferent coupling could be applied also on packages and types. For example the efferent coupling for a particular type is the number of types it directly depends on. Types where TypeCe is very high are types that depend on too many other types. They are complex and in general have more than one responsibility.


The ratio of the number of internal abstract types (i.e abstract classes and interfaces) to the number of internal types. The range for this metric is 0 to 1, with A=0 indicating a completely concrete project and A=1 indicating a completely abstract project

    A = Na / Nc


    A = abstractness of a module
    Zero is a completely concrete module. One is a completely abstract module.
    Na = number of abstract classes in the module.
    Nc = number of concrete classes in the module.

Let’s take as example the neo4j-kernel-1.8.2 jar and search for all abstract types.

from t in Types where t.IsAbstract || t.IsInterface
select new { t, t.NbLinesOfCode }


neo4j-kernel-1.8.2 has 1071 types so the Abstractness is equal to 233/1071 = 0.21755

To increase the abstractness of a project we have to add more abstract classes or interfaces.

The ratio of efferent coupling (Ce) to total coupling. I = Ce / (Ce + Ca). This metric is an indicator of the project’s resilience to change. The range for this metric is 0 to 1, with I=0 indicating a completely stable project and I=1 indicating a completely instable project.

I = Ce/(Ce + Ca)
I represent the degree of instability associated with a project.
Ca represents the afferent coupling, or incoming dependencies, and
Ce represents the efferent coupling, or outgoing dependencies

Let’s take as example a class where many other classes used it and it not use any other class. In this case this class is considered as stable for the following reasons:

- This kind of class depends upon nothing at all, so a change from a depended cannot ripple up to it and cause it to change. This characteristic is called “Independence”. Independent classes are classes which do not depend upon anything else.

- It’s depended upon by many other classes. It became harder to make changes to it. And if we were to change it we would have to change all the other classes that depended upon it. Thus, there is a great deal of force preventing us from changing these classes, and enhancing their stability.

Classes that are heavily depended upon are called “Responsible”. Responsible classes tend to be stable because any change has a large impact.

Let’s search for the more responsible types by executing the following CQLinq query

(from t in Types
orderby t.NbTypesUsingMe descending, t.NbTypesUsed descending, t.NbBCInstructions descending
select new { t, t.NbTypesUsingMe,t.NbTypesUsed})


The Expression class is the most popular one.

Abstractness vs Instability Graph and the zone of pain

To have more details about this graph you can refer to the Robert C.Martin article.

Here’s the graph for Neo4j framework


The idea behind this graph is that the more a code element of a program is popular, the more it should be abstract. Or in other words, avoid depending too much directly on implementations, depend on abstractions instead. By popular code element I mean a project (but the idea works also for packages and types) that is massively used by other projects of the program.
It is not a good idea to have concrete types very popular in your code base. This provokes some Zones of Pains in your program, where changing the implementations can potentially affect a large portion of the program. And implementations are known to evolve more often than abstractions.

The main sequence line (dotted) in the above diagram shows the how abstractness and instability should be balanced. A stable component would be positioned on the left. If you check the main sequence you can see that such a component should be very abstract to be near the desirable line – on the other hand, if its degree of abstraction is low, it is positioned in an area that is called the “zone of pain”.

For example neo4j kernel has many classes depending on it, so it’s positioned on the left and in this case it’s preferable to be more abstract to leave the orange zone and goes to the green zone.

What’s important is to avoid the zone of pain, if a jar is inside this zone, any changes to it will impact a lot of classes and it became hard to maintain or evolve this module.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.


Published at DZone with permission of Issam Lahlali. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}