Object Oriented Information Systems

Packing and Testing

To what extent is cyclomatic complexity relevant when developing object-oriented code

In general, cyclomatic complexity is associated with supporting testing processes and helping reduce code complexity. In testing, the more execution paths are counted, the more test case can be written to cover all possible variations in the code execution (Schults, 2021).

In object-oriented code, cyclomatic complexity can help to determine the level of changes the code will suffer through the releases of new versions. The higher the cyclomatic complexity, the lower the changes the code suffers between versions (Vasa & Schneider, 2003).

References
Schults, C. (2021) Cyclomatic Complexity Defined Clearly, With Examples.  Available from https://linearb.io/blog/cyclomatic-complexity/  [Accessed 07 April 2023].

Vasa, R & Schneider, J. (2003) Evolution of Cyclomatic Complexity in Object Oriented Software. Available from: https://www.researchgate.net/publication/238472345 [Accessed 07 April 2023].


What is the cyclomatic complexity of the following piece of code?

public static string IntroducePerson(string name, int age)
{
   var response = $"Hi! My name is {name} and I'm {age} years old.";

   if (age >= 18)
      response += " I'm an adult.";

   if (name.Length > 7)
      response += " I have a long name.";

   return response;
}



Cyclomatic complexity
The above function has a cyclomatic complexity of 3. The are only 3 decision to be counted and this number indicates low complexity.