© 2020 Hee-Meng Foo
Cyclomatic & Cognitive
Complexity
Hee-Meng Foo, April 2020
© 2020 Hee-Meng Foo
What is Cyclomatic Complexity
● Proposed by Thomas McCabe in 1976 to measure how complex a piece of
code is
○ Complexity - measure of testability and maintainability of a piece of code
○ Main idea
■ build a DAG representing the control flow of a function/module - Control Flow Graph (CFG)
■ Measure the number of linearly independent paths through the code
○ Formula: M = E - N + 2P
● Historical context: this was the time of BASIC, ALGOL, FORTRAN, COBOL
○ The dreaded GOTO statement
● Correlates to number of defects (see Wikipedia for references)
● Relates to testing
○ It provides an upper bound to the num of test cases to achieve complete branch coverage
○ It provides a lower bound to the num of test cases for path coverage
© 2020 Hee-Meng Foo
Building the CFG (Control Flow Graph)
© 2020 Hee-Meng Foo
Calculating the Cyclomatic Complexity
M = E - N + 2P
E = num edges
N = num nodes
P = num connected components
M = 11 - 9 + 2(1)
= 4
© 2020 Hee-Meng Foo
Calculating the Cyclomatic Complexity
M = num of flow control cmds + 1
= 3 + 1 = 4
Watch the YouTube video found in References [6]
© 2020 Hee-Meng Foo
More examples of Control Flows
Source: YouTube (see References [1])
© 2020 Hee-Meng Foo
Rule of Thumb
● 1-10 : simple function, not much risk
● 11-20 : Moderate risk
● 21-50 : High risk
● > 50 : really bad
Source: http://www.ganssle.com/articles/cyclomaticcomplexity.html
© 2020 Hee-Meng Foo
What is Cognitive Complexity
● Proposed by G. Ann Campbell of SonarSource (2018)
● The problem with Cyclomatic Complexity:
○ Addresses testability but not maintainability
○ Does not take into account modern code constructs like switch, try-catch, lambdas, ternary
operators in making code easier to comprehend (hence cognitive complexity)
○ OOP: a class with many easily maintained methods may have same complexity as a class with
1 method with high complexity
“If you’re having to fix a P0 at 3am in the morning, what kind of code would you like
to be troubleshooting?”
© 2020 Hee-Meng Foo
Differences in Calculation
1. Ternary operators add 0 to the complexity
a. eg. boolean a = x > y ? true : false
b. The equivalent if-else will add 1
2. Switch statements only add 1 to the complexity regardless of num cases
3. Try-catch statements add 1 to complexity regardless of num catches
4. Predicates:
a. If you break the linear flow, you add to complexity
b. Eg. (a && b && c) adds 1 but (a && b || c) adds 2
5. continue, break don’t add to complexity
6. Nested flows accumulate
Source: YouTube (see References [5])
© 2020 Hee-Meng Foo
Differences in Calculation
Class Methods
Source: SonarSource Blog (see References [7])
© 2020 Hee-Meng Foo
So How Do You Apply Cyclomatic & Cognitive Complexity?
● Should they be used as gating criteria?
Probably not. There are many reasons why code is sometimes complex.
Of course a high complexity number of ~ 50 should raise some red flags
● Should they be used as input to Code Reviews?
Most definitely. The principle of enlightened self interest should hold.
In other words, if you do not want to debug/maintain highly complex code, you
should call it out when reviewing PRs.
● Should you prioritize tests based on complexity?
Most definitely. The more complex the code, the more it needs bubble wrap!
© 2020 Hee-Meng Foo
References
1. Wikipedia entry on Cyclomatic Complexity -
https://en.wikipedia.org/wiki/Cyclomatic_complexity
2. YouTube: Cyclomatic Complexity for Developers by Jeroen Resoort
(https://youtu.be/JwTQywqpZ5Y)
3. Taming Complexity by Jack Ganssle -
http://www.ganssle.com/articles/cyclomaticcomplexity.html
4. Cognitive Complexity: A new way of measuring understandability by G. Ann Campbell -
https://www.sonarsource.com/docs/CognitiveComplexity.pdf
5. YouTube: Time to learn Cognitive Complexity by Yashish Dua (https://youtu.be/r-
LCSRxJSMI)
6. YouTube: Cyclomatic Complexity in Software Engineering with Trick by Arora Education
(https://youtu.be/PlCGomvu-NM)
7. SonarSource Blog on Cyclomatic Complexity - https://blog.sonarsource.com/cognitive-
complexity-because-testability-understandability

Cyclomatic and cognitive complexity

  • 1.
    © 2020 Hee-MengFoo Cyclomatic & Cognitive Complexity Hee-Meng Foo, April 2020
  • 2.
    © 2020 Hee-MengFoo What is Cyclomatic Complexity ● Proposed by Thomas McCabe in 1976 to measure how complex a piece of code is ○ Complexity - measure of testability and maintainability of a piece of code ○ Main idea ■ build a DAG representing the control flow of a function/module - Control Flow Graph (CFG) ■ Measure the number of linearly independent paths through the code ○ Formula: M = E - N + 2P ● Historical context: this was the time of BASIC, ALGOL, FORTRAN, COBOL ○ The dreaded GOTO statement ● Correlates to number of defects (see Wikipedia for references) ● Relates to testing ○ It provides an upper bound to the num of test cases to achieve complete branch coverage ○ It provides a lower bound to the num of test cases for path coverage
  • 3.
    © 2020 Hee-MengFoo Building the CFG (Control Flow Graph)
  • 4.
    © 2020 Hee-MengFoo Calculating the Cyclomatic Complexity M = E - N + 2P E = num edges N = num nodes P = num connected components M = 11 - 9 + 2(1) = 4
  • 5.
    © 2020 Hee-MengFoo Calculating the Cyclomatic Complexity M = num of flow control cmds + 1 = 3 + 1 = 4 Watch the YouTube video found in References [6]
  • 6.
    © 2020 Hee-MengFoo More examples of Control Flows Source: YouTube (see References [1])
  • 7.
    © 2020 Hee-MengFoo Rule of Thumb ● 1-10 : simple function, not much risk ● 11-20 : Moderate risk ● 21-50 : High risk ● > 50 : really bad Source: http://www.ganssle.com/articles/cyclomaticcomplexity.html
  • 8.
    © 2020 Hee-MengFoo What is Cognitive Complexity ● Proposed by G. Ann Campbell of SonarSource (2018) ● The problem with Cyclomatic Complexity: ○ Addresses testability but not maintainability ○ Does not take into account modern code constructs like switch, try-catch, lambdas, ternary operators in making code easier to comprehend (hence cognitive complexity) ○ OOP: a class with many easily maintained methods may have same complexity as a class with 1 method with high complexity “If you’re having to fix a P0 at 3am in the morning, what kind of code would you like to be troubleshooting?”
  • 9.
    © 2020 Hee-MengFoo Differences in Calculation 1. Ternary operators add 0 to the complexity a. eg. boolean a = x > y ? true : false b. The equivalent if-else will add 1 2. Switch statements only add 1 to the complexity regardless of num cases 3. Try-catch statements add 1 to complexity regardless of num catches 4. Predicates: a. If you break the linear flow, you add to complexity b. Eg. (a && b && c) adds 1 but (a && b || c) adds 2 5. continue, break don’t add to complexity 6. Nested flows accumulate Source: YouTube (see References [5])
  • 10.
    © 2020 Hee-MengFoo Differences in Calculation Class Methods Source: SonarSource Blog (see References [7])
  • 11.
    © 2020 Hee-MengFoo So How Do You Apply Cyclomatic & Cognitive Complexity? ● Should they be used as gating criteria? Probably not. There are many reasons why code is sometimes complex. Of course a high complexity number of ~ 50 should raise some red flags ● Should they be used as input to Code Reviews? Most definitely. The principle of enlightened self interest should hold. In other words, if you do not want to debug/maintain highly complex code, you should call it out when reviewing PRs. ● Should you prioritize tests based on complexity? Most definitely. The more complex the code, the more it needs bubble wrap!
  • 12.
    © 2020 Hee-MengFoo References 1. Wikipedia entry on Cyclomatic Complexity - https://en.wikipedia.org/wiki/Cyclomatic_complexity 2. YouTube: Cyclomatic Complexity for Developers by Jeroen Resoort (https://youtu.be/JwTQywqpZ5Y) 3. Taming Complexity by Jack Ganssle - http://www.ganssle.com/articles/cyclomaticcomplexity.html 4. Cognitive Complexity: A new way of measuring understandability by G. Ann Campbell - https://www.sonarsource.com/docs/CognitiveComplexity.pdf 5. YouTube: Time to learn Cognitive Complexity by Yashish Dua (https://youtu.be/r- LCSRxJSMI) 6. YouTube: Cyclomatic Complexity in Software Engineering with Trick by Arora Education (https://youtu.be/PlCGomvu-NM) 7. SonarSource Blog on Cyclomatic Complexity - https://blog.sonarsource.com/cognitive- complexity-because-testability-understandability