SECTION 05 / 6

Module 5 · Software Metrics

3 min read
On this page 9 headings

Learning goals: Use quantitative measurements of complexity, source lines and comments, alongside qualitative review.

Why Clean Code Matters

Clean code is easier for team members to understand, easier to test and debug, and easier to maintain or extend later. A program that produces the right answer today can still create problems for the team if its structure is confusing or full of duplication.

Figure 52. Why Clean Code Matters.
Figure 52. Why Clean Code Matters.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: Correct behavior matters, but clear structure matters too.

Quantitative Metrics vs Qualitative Review

Software quality can be examined with numbers and with human judgement. Metrics count properties such as lines, complexity and comments. Reviews look at clarity, naming and how easy the program is to understand. A single score cannot describe every important quality of a software design or implementation.

Figure 53. Quantitative Metrics vs Qualitative Review.
Figure 53. Quantitative Metrics vs Qualitative Review.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: Combine quantitative measurements with qualitative code review.

Cyclomatic Complexity: Decisions Plus One

Cyclomatic complexity is a score per function: decision points plus one. A single if gives 1 + 1 = 2. A function with CC above 41 is very error-prone and unstable; reduce CC by splitting the function into multiple functions and refactoring.

Figure 54. Cyclomatic Complexity: Decisions Plus One.
Figure 54. Cyclomatic Complexity: Decisions Plus One.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: For a simple example: CC = decisions + 1.

What Increases Cyclomatic Complexity?

Conditional statements and loop conditions add decision points. Each if, additional elif, for or while condition can add one decision. An else is the alternative path of an existing decision, not a new decision by itself.

Figure 55. What Increases Cyclomatic Complexity?
Figure 55. What Increases Cyclomatic Complexity?
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: Count decisions, not merely the number of lines.

Radon Measures Prime-Function Complexity

Radon is a Python tool for inspecting software metrics. The lecture runs it on a first_n_primes.py example and receives cyclomatic complexity 5 with an A rank, indicating low complexity for that function under Radon's grading rules. This is a property of one code block, not a final quality rating for the whole application.

Figure 56. Radon Measures Prime-Function Complexity.
Figure 56. Radon Measures Prime-Function Complexity.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: The lecture's measured result is CC = 5, grade A.

Raw Metrics: LOC vs LLOC vs SLOC

Raw metrics: LOC 26, LLOC 17, SLOC 18, comments 5, blanks 3. The example splits one for statement over two lines with a backslash; joining it makes LLOC = SLOC. Radon also reports the comment-to-SLOC ratio.

Figure 57. Raw Metrics: LOC vs LLOC vs SLOC.
Figure 57. Raw Metrics: LOC vs LLOC vs SLOC.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: 26 total lines = 18 source lines + 5 comment lines + 3 blank lines.

Halstead: Operators and Operands

Halstead: h1 is the number of distinct operators; in the lecture’s example: <, %, an increment, another increment → 4. h2 counts distinct operands; N1 and N2 count total operators and operands. Radon derives vocabulary, length, volume, difficulty and effort from h1, h2, N1, N2 (not covered in detail).

Figure 58. Halstead: Operators and Operands.
Figure 58. Halstead: Operators and Operands.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: Operators act on values; operands are the values or names being used.

Radon Checks a Flask Codebase

Flask flashcard example: run radon cc on the controller. Function hello (line 9) has CC 9 — “not too bad”, but several fors and nested ifs add complexity; split overly complex functions. radon raw shows only a 6% comment ratio, meaning more comments should be added. The API file also has a low comment ratio.

Figure 59. Radon Checks a Flask Codebase.
Figure 59. Radon Checks a Flask Codebase.
Open image ↗
Illustration: Module 5 · Software Metrics

Remember: Use Radon to examine complexity and raw metrics.

Quick revision and self-check

Question: What is the example complexity with one simple if?

Answer: 2: one decision plus one.

Question: What were the raw counts shown in the lecture?

Answer: LOC 26, SLOC 18, LLOC 17, comments 5, blanks 3.

Question: Are metrics a complete measure of quality?

Answer: No. Readability and human review still matter.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑