Background
While writing professional code, code metrics are an important tool to determine that you are writing quality code that would make it easy to test, understand and maintain in the long run, as the code passes from one developer to another. Since every developer has a different style of writing the same logic, it becomes imperative to set up some standard/guidelines or quantitative measures to tell or determine what makes a good quality code. Two of the most relevant ones, that I find very interesting to explore is,
Cyclomatic Complexity and it's younger sibling Cognitive Complexity!
The concept of Cognitive Complexity was brought in by SonarQube. They wanted to introduce a more contextualized form of measuring the code complexity. While you can read all the artifacts available to you in public domain on both this topic, I would rather summarize it as below, to the best of my understanding of the concepts till now,
Cyclomatic Complexity
Measures, how difficult it is test the code (i.e. Testability). Alternatively, this measure is a hint of how many distinct test cases you should write to have 100% code coverage. 

Cognitive Complexity
Measures, how difficult it is to understand or read the code (i.e. human readability).
Let me try to explain this using a small example. Consider the following Lines of Code,
- switch (input)
- {
- case 1:
- //do something
- break;
- case 2:
- //do something else
- break;
- }
As per the Cyclomatic complexity, it would measure this as 2. That's because, you would probably write two test cases, one to test each of the cases individually. While this is too simplistic an example to put the point, extrapolate this in your mind, with all the conditional flows, loops etc that you may have written as part of the same function/method. The score would increase linearly. Thus, as per the general score guidelines,
| Score | Cyclomatic | Risk Type |
| 1 to 10 | Simple | Not much risk |
| 11 to 20 | Complex | Low risk |
| 21 to 50 | Too complex | Medium risk, attention |
| More than 50 | Too complex | Can't test, high risk |
But as per Cognitive Complexity, it would measure this as 1 for all of the switch case options. Remember, it's algorithm measures the readability. The purpose of the switch case is to present multiple lanes of processing and therefore having multiple cases is not complex to comprehend. It is expected to be the nature of the switch capability. An acceptable value is 15 or less for a given method as set by many professional software houses.
Now let's start our play to understand the difference of the two. I will work with a simple program of finding whether the input given, is a leap year or not. So, the given year is a leap year, if it's ordinal is exactly divisible by 4. However, if it is also divisible by 100, but not by 400, then it's no longer a leap year. Finally, if it is divisible by 400 as well, then it's again a leap year. Therefore, the test cases are,
Leap Year : 1980, 1996, 2000, 2400 etc.
Not a leap year: 1981, 1995, 1700, 1900 etc.
My first raw algorithm is as follows: (I have restricted to taking the input between 1000 and 9999)
- public bool IsLeapYear(int y)
- {
- bool response = false;
- if ((y >= 1000) && (y <= 9999))
- {
- //if it is divisible by 400 e.g. 1600, 2000, then all other checks are useless. we can return from here
- if (y % 400 == 0)
- response = true;
- //for years like 1700, 1900, 2100 etc. which are not leap years
- else if ((y % 100 == 0) && (y % 400 != 0))
- response = false;
- //finally, if the above two fails, just check if it divisible by 4 for years like 1980,1996 etc
- else if (y % 4 == 0)
- response = true;
- }
- else
- {
- response = false;
- throw new ArgumentOutOfRangeException("year", " The argument value should be inside 1000 and 9999 ");
- }
- return response;
- }
The Cyclomatic Complexity of the above function is 7. It is measured at every point that I highlighted using the RED-tangle below. Basically, you should be writing so many test cases to test this function and have 100% code coverage

For Cognitive Complexity, the measuring algorithm would be slightly different,




Join the conversation! Your thoughts help the community grow.