Refactoring with Cognitive Complexity the New Option for Measuring - - PowerPoint PPT Presentation

refactoring with cognitive complexity
SMART_READER_LITE
LIVE PREVIEW

Refactoring with Cognitive Complexity the New Option for Measuring - - PowerPoint PPT Presentation

Refactoring with Cognitive Complexity the New Option for Measuring Understandability G. Ann Campbell @GAnnCampbell GenevaJug Cognitive Complexity Introduced Dec. 2016 Available as a rule in SonarQube analyzers SonarC#


slide-1
SLIDE 1

GenevaJug

  • G. Ann Campbell

@GAnnCampbell

Refactoring with

Cognitive Complexity

the New Option for Measuring Understandability

slide-2
SLIDE 2

Cognitive Complexity

  • Introduced Dec. 2016
  • Available as a rule in SonarQube analyzers

○ SonarC# ○ SonarCFamily ○ SonarJava ○ SonarJS ○ …

  • https://www.sonarsource.com/docs/CognitiveComplexity.pdf
slide-3
SLIDE 3

Cyclomatic Complexity

Intended to measure

  • Testability
  • Maintainability
slide-4
SLIDE 4

Cyclomatic Complexity

int sumOfPrimes(int max) { // +1 int total = 0; OUT: for (int i = 1; i <= max; ++i) { // +1 for (int j = 2; j < i; ++j) { // +1 if (i % j == 0) { // +1 continue OUT; } } total += i; } return total; } // Cyclomatic Complexity 4

String getWords(int number) { // +1 switch (number) { case 1: // +1 return "one"; case 2: // +1 return "a couple"; case 3: // +1 return "a few"; default: return "lots"; } } // Cyclomatic Complexity 4

slide-5
SLIDE 5

Cognitive Complexity Motivations

  • 1. Measure “understandability”
  • 2. Incent good code
slide-6
SLIDE 6

Guiding Principles

slide-7
SLIDE 7

Guiding Principles

  • 1. Ignore readable shorthand structures
slide-8
SLIDE 8

Guiding Principles

  • 1. Ignore readable shorthand structures
  • Method Structure
  • Null-coalescing operators
slide-9
SLIDE 9

Guiding Principles

  • 1. Ignore readable shorthand structures
  • Method Structure
  • Null-coalescing operators

MyObj myObj = null; if (a != null) { myObj = a.myObj; } MyObj myObj = a?.myObj;

slide-10
SLIDE 10

Guiding Principles

  • 1. Ignore readable shorthand structures
  • 2. Increment for breaks in linear flow
slide-11
SLIDE 11

Guiding Principles

  • 1. Ignore readable shorthand structures
  • 2. Increment for breaks in linear flow
  • if, else if, else, ternary operators
  • switch
  • catch
  • Loops
  • Sequences of logical operators (a && b && c && d // +1)
  • Jumps to labels
  • Recursion
slide-12
SLIDE 12

Guiding Principles

  • 1. Ignore readable shorthand structures
  • 2. Increment for breaks in linear flow
  • 3. Increment for nesting
slide-13
SLIDE 13

Guiding Principles

  • 1. Ignore readable shorthand structures
  • 2. Increment for breaks in linear flow
  • 3. Increment for nesting
  • Loops
  • switch
  • catch
  • if and ternary
slide-14
SLIDE 14

Guiding Principles

  • 1. Ignore readable shorthand structures
  • 2. Increment for breaks in linear flow
  • 3. Increment for nesting
  • Loops
  • switch
  • catch
  • if and ternary
  • else if, else increment nesting level
  • Nested method / lambda increments nesting level
slide-15
SLIDE 15

Cognitive Complexity

int sumOfPrimes(int max) { int total = 0; OUT: for (int i = 1; i <= max; ++i) { // +1 for (int j = 2; j < i; ++j) { // +2 if (i % j == 0) { // +3 continue OUT; // +1 } } total += i; } return total; } // Cognitive Complexity 7

String getWords(int number) { switch (number) { // +1 case 1: return "one"; case 2: return "a couple"; case 3: return "a few"; default: return "lots"; } } // Cognitive Complexity 1

slide-16
SLIDE 16

Real life code

slide-17
SLIDE 17

Recap

  • Cognitive Complexity: understandability

○ Increments for breaks in linear flow ○ Increments for nesting ○ Ignores readable shorthand structures

  • High method Cognitive Complexity indicates

a need for refactoring

slide-18
SLIDE 18

Questions

@GAnnCampbell