Break the spaghetti code with these 5 useful metrics

By October 20, 2020
Break the spaghetti code with these 5 useful metrics

Have you ever been panicked or exasperated by an illegible code?

We offer you to get out of this anxiety-provoking cycle and thus, equipped with the right method and a good tool, to carry out the maintenance and evolution of complex codes easily.

What is Spaghetti code ?

The spaghetti code refers to a code that is difficult to read and maintain. The code has the following characteristics: - an important nesting or dispersion of the logic (trade) - a complex control structure - complex logic checks - a potentially abundant presence of comments - a code that does several things at once (Single Responsiblity Principle Violation)

Fortunately, it is easy to identify this type of code with a good tool.

5 useful metrics to detect spaghetti code

Here are the 5 useful metrics we recommend to detect the spaghetti code

1 - The code coverage rate

The coverage is an excellent way to identify our spaghetti code. A complex code will probably be poorly tested since it requires a tremendous effort to the developer.

Reporting Code Coverage Using Maven and JaCoCo Plugin - DZone Performance Reporting Code Coverage Using Maven and JaCoCo Plugin - DZone Performance\

The measurement of code coverage is a metric to inform the developer about the completeness of these tests.

There are four levels for assessing whether a code is well tested:

  • coverage by lines of code
  • coverage of execution paths
  • coverage of predicates
  • data coverage

The effort to cover the four levels is cumulative : to cover all the paths, you cover all the lines…

No matter what your tool uses as an indicator of code coverage, spaghetti code is statistically poorly covered because the difficulty to cover it increase with its complexity.

Advice : look for poorly covered code.

2 - Code complexity

Since our spaghetti code is unreadable, it is also complex to read. Fortunately several metrics are there to find this nasty little piece of code.

Cyclomatic complexity, NPath… there are many metrics. The importance is to understand what it measures and specifically how to improve results.

 

Code complexity Code complexity with Embold\

The Cyclomatic complexity (abbreviated CC) evaluates the potential number of paths to compute ( explanation here  and here). It is often simplified by 1 + the number of control flow branches. Each path is a test case. Some classes like in the previous figure requires more than 300 test cases to assess the functionalities. An if statement counts for 2, a loop for 2, a switch for n cases.

The NPath complexity evaluates the complexity taking into account both paths (like sCC) and the combinatorics linked to the path imbrication. More a code is nested, higher is the complexity ( exponential ).

The more complex the code is, the greater the effort to test it. Testability and maintenability go hand in hand.

Advice : To find the spaghetti code, look for the complex code of your application.

3 - Coupling Between Objects (CBO)

Coupling is the notion of dependence between two objects (or classes) that creates a strong relationship. If one class is modified, there is a strong chance of creating a regression in the other.

A spaghetti code can focus attention on itself either by using many resources of the application or by being critical and used by everyone.

The CBO metric evaluates the level of dependency between this class and the rest of the application.

A strongly coupled, potentially complex class is most likely spaghetti code that is difficult to isolate and extract from the application.

A huge (in terms of lines) and highly dependent class in your application is often nick named as a god class as on the figure below :

 

GodClass : evaluation by Embol GodClass : evaluation by Embol\

4 - Number of LOC in the method

Large classes, large methods require large efforts of understanding.

On the basis of this observation, it is important to quickly identify methods that are too voluminous and require time to understand. The ones which probably violate the SRP principle and therefore potentially the spaghetti code.

Metric : line of codes Metric : line of codes\

What size  is indicative of a spaghetti code? Look for the largest ones and go down the hierarchy until you find harmless codes.

5 - Response for Class (RFC)

A spaghetti code as opposed to a pure function has many side effects. The code manipulates many objects and causes state changes in several classes and systems around it.

Potential side effects can be evaluated via the RFC metric.

The RFC metric counts the number of potentially executed methods within a code to determine its complexity.

Code that invokes many other methods (ideally other classes) is fragile and potentially difficult to understand or test.

RFC : methode dependencies RFC : methode dependencies\

Conclusion and a case of study

I recommend the opensource project OpenKM as a source of understanding while learning metrics. It contains plenty of Java code from different ages and quality.

To learn how to deal with spaghetti code,  pick a tool with the described metrics. They are plenty. Here in the example, I used Embold since it was free and simple to scan the code.

As a conclusion; if you liked this article, subscribe, comment and do not miss the following articles about how to fix a spaghetti code!

Prefer ravioli code rather than spaghetti code Prefer ravioli code rather than spaghetti code (from Pexels)\