$30 off During Our Annual Pro Sale. View Details »

Cyclomatic Complexity

Cyclomatic Complexity

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

Anna Filina
PRO

October 08, 2016
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. Cyclomatic complexity =
    edges − nodes +

    2 * connected components

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. Cyclomatic complexity =
    9 − 8 + 2 * 1 =
    3

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. @afilina afilina.com

    View Slide