Cyclomatic Complexity

Cyclomatic Complexity

Don’t write unnecessary tests anymore. Use a simple, scientific approach.

B3b2139e4f2c0eca4efe2379fcebc1c5?s=128

Anna Filina

October 08, 2016
Tweet

Transcript

  1. foolab.ca | @foolabca Cyclomatic Complexity Bulgaria PHP, Sofia - October

    8, 2016
  2. Execution Paths public function getShipping() { if ($this->subtotal >= 40)

    { return 0; } return $this->standardShipping; } 2
  3. Execution Paths public function getShipping() { if ($this->subtotal >= 40)

    { return 0; } return $this->standardShipping; } 3 Path 1 Path 2
  4. Decision Branches 4 start product loop free shipping? end

  5. Decision Branches 5 start product loop free shipping? end

  6. Cyclomatic complexity = edges − nodes +
 2 * connected

    components
  7. Decision Branches 7 start product loop free shipping? end ◦

    Edges ◦ Nodes ◦ Connected components.
  8. Decision Branches 8 start product loop free shipping? end ◦

    Edges ◦ Nodes ◦ Connected components.
  9. Decision Branches 9 start product loop free shipping? end ◦

    Edges ◦ Nodes ◦ Connected components.
  10. Cyclomatic complexity = 9 − 8 + 2 * 1

    = 3
  11. Zero Times foreach ($products as $product) { $total = $product->price

    * $product->quantity; } $tax = $total * ... 11
  12. Multiple Times $total = 0; foreach ($products as $product) {

    $total = $product->price * $product->quantity; } $tax = $total * ... 12
  13. Multiple Times $total = 0; foreach ($products as $product) {

    $total += $product->price * $product->quantity; } $tax = $total * ... 13
  14. @afilina afilina.com