Code metrics

The static measures of a piece of code and their usual thresholds: lines of code, cyclomatic complexity, cognitive complexity, coupling, cohesion, duplication, technical debt, and what a metric does not tell you.

  • 28 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. Understand

A few cards from the deck

  • Cyclomatic complexity: what is it?

    Show the answer

    The number of independent paths through a function's control-flow graph: the number of decision points, plus one.

    It gives the minimum number of tests needed to cover each independent path. McCabe recommends staying under 10: beyond that, the function becomes hard to test.

  • What is the cyclomatic complexity of this function? Count if, elif, for, while, and, or, then add 1.

    ```python
    def classer(notes, seuil):
    total = 0
    for n in notes:
    if n < 0 or n > 20:
    continue
    if n >= seuil:
    total += 1
    elif n >= seuil - 2 and n > 5:
    total += 0.5
    return total
    `

    • 7
    • 5
    • 6
    • 9
    Show the answer

    7

    Six decision points: for, if, or, if, elif, and. Plus 1 makes 7. Forgetting the or and and operators, which each create a branch, gives 5; forgetting the +1 gives 6.

  • On a control-flow graph with E edges and N nodes, the cyclomatic complexity is ….

    Show the answer

    On a control-flow graph with E edges and N nodes, the cyclomatic complexity is E − N + 2.

    For a function with no decision, there are N − 1 edges and the formula gives 1: a single path. Each decision adds one more edge than nodes, hence 1 to the complexity.

  • Cognitive complexity: what is it?

    Show the answer

    A measure of the effort needed to understand a function, which penalises breaks in flow and above all their nesting.

    Ten if statements in a row are easy to read; ten nested if statements are not, yet the cyclomatic complexity is the same. The cognitive one tells them apart.