How Readable Is Your Code? Part 1
Let's write a simple application and create its flowchart. Application logic is not important. Find out more about code readability!
Join the DZone community and get the full member experience.
Join For FreeThere Is No Perfect Implementation?
Every developer has his own preferences and vision about problem-solving. Any problem can be solved in my différent ways following know practices like SOLID, KISS, etc. But how to compare 2 different implementations? Smaller is better? Only Object-Oriented? How practically evaluate notions like code maintainability, readability, transparency? Well, I'm not sure that there is absolute truth in such questions, but in this article, you will find a metric that can help you find out.
Cyclomatic Complexity — Number of Scenarios in Code.
Cyclomatic complexity is a metric invented to find out the number of tests to cover the given code fully. This metric also can be used to measure the readability of your code. Cyclomatic Complexity shows how many scenarios consist of your code (or how many independent ways inside its graph)
Cyclomatic Complexity Example
Let's write a simple application and create its flowchart. Application logic is not important. Application iterates applicants and calculates their salary. For java developers salary is increased by 2 times.
x
public static void main(String[] args) {
//statement 1
List<Applicant> applicants = Arrays.asList(new Applicant("John", 5, true),
new Applicant("Mary", 10, true));
for (Applicant applicant : applicants) { //Condition 1
//statement 2
int salary = 0;
if (applicant.isJavaDev()) { //condition 2
//statement 3
salary = 2 * 10 * applicant.getYearOfExperience();
log.info(applicant.getName() + " salary is : " + salary);
} else {
//statement 4
salary = 10 * applicant.getYearOfExperience();
log.info(applicant.getName() + " salary is : " + salary);
}
}
}
static class Applicant {
private String name;
private int yearOfExperience;
private boolean isJavaDev;
}
Calculating Cyclomatic Complexity
To calculate Cyclomatic Complexity, we need to create the code's flowchart.
In the image, you can see that code contains 7 nodes and 8 edges. The following expression calculates cyclomatic complexity (CYC):
CYC = (Number of Edges) — (Number of Nodes) + 2 * (Number of Exit Nodes)
For our case, CYC is:
CYC = 8 — 7 + 2 * 1 = 3
Cyclomatic complexity is 3. But what does it mean? This number describes how many independent scenarios in our code. Let's find all of them:
1. There are no applicants — so we skip iteration and exit application (Greenway)
2. Applicant is not a java developer (So 4th statement is performed- Blue way)
3. Applicant is java developer (So 3rd statement is performed — Red way)
The factor that increases complexity:
- If/Switch/Recursion/Label jumps/
- Iterations (For each, While)
- Complex conditions increase complexity eg (condition1 || condition2) add 2 score.
Can We Recognize Cyclomatic Complexity as Readability Metric?
Well, in some sort. If an implementation can reduce cyclomatic complexity without losing functionality, it means that it has extra instructions. In general relation between code readability/maintainability and CYC are described in the next table:
CYC SCORE | LEVEL OF READABILITY |
---|---|
1-10 | Maintainable code |
10-20 | Difficult to support |
20-40 | Very difficult to support |
>40 | Unmaintainable code |
But What About Better Metrics? Find in the Second Part
Cyclomatic Complexity metric doesn't cover human aspects in code readability. In the next chapter, I'll introduce different metrics that might be more practical.
Opinions expressed by DZone contributors are their own.
Comments