Debian Codegraph @

docs/intro.rst

raw
1 Introduction to Code Metrics
2 ============================
3
4 This section contains a brief explanations of the metrics that Radon can
5 compute.
6
7
8 Cyclomatic Complexity
9 ---------------------
10
11 Cyclomatic Complexity corresponds to the number of decisions a block of code
12 contains plus 1. This number (also called McCabe number) is equal to the number
13 of linearly independent paths through the code. This number can be used as a
14 guide when testing conditional logic in blocks.
15
16 Radon analyzes the AST tree of a Python program to compute Cyclomatic
17 Complexity. Statements have the following effects on Cyclomatic Complexity:
18
19 ================== ============== ===========================================================================================
20 Construct Effect on CC Reasoning
21 ================== ============== ===========================================================================================
22 if +1 An `if` statement is a single decision.
23 elif +1 The `elif` statement adds another decision.
24 else +0 The `else` statement does not cause a new decision. The decision is at the `if`.
25 case pattern +1 The `case pattern` statement is a single decision.
26 case _ +0 The `case _` statement does not cause a new decision. The decision is at the `case pattern`.
27 for +1 There is a decision at the start of the loop.
28 while +1 There is a decision at the `while` statement.
29 except +1 Each `except` branch adds a new conditional path of execution.
30 finally +0 The finally block is unconditionally executed.
31 with +1 The `with` statement roughly corresponds to a try/except block (see PEP 343 for details).
32 assert +1 The `assert` statement internally roughly equals a conditional statement.
33 Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
34 Boolean Operator +1 Every boolean operator (and, or) adds a decision point.
35 ================== ============== ===========================================================================================
36
37
38 Maintainability Index
39 ---------------------
40 Maintainability Index is a software metric which measures how maintainable
41 (easy to support and change) the source code is. The maintainability index is
42 calculated as a factored formula consisting of SLOC (Source Lines Of Code),
43 Cyclomatic Complexity and Halstead volume. It is used in several automated
44 software metric tools, including the Microsoft Visual Studio 2010 development
45 environment, which uses a shifted scale (0 to 100) derivative.
46
47 Common formulas are:
48
49 * the original formula:
50
51 .. math::
52
53 MI = 171 - 5.2 \ln V - 0.23 G - 16.2 \ln L
54
55 * the derivative used by SEI:
56
57 .. math::
58
59 MI = 171 - 5.2\log_2 V - 0.23 G - 16.2 \log_2 L + 50 \sin(\sqrt{2.4 C})
60
61 * the derivative used by Visual Studio:
62
63 .. math::
64
65 MI = \max \left [ 0, 100\dfrac{171 - 5.2\ln V - 0.23 G - 16.2 \ln L}{171} \right ].
66
67 Radon uses another derivative, computed from both SEI derivative and Visual
68 Studio one:
69
70 .. math::
71
72 MI = \max \left [ 0, 100\dfrac{171 - 5.2\ln V - 0.23 G - 16.2 \ln L + 50 \sin(\sqrt{2.4 C}))}{171} \right ]
73
74 Where:
75 * ``V`` is the Halstead Volume (see below);
76 * ``G`` is the total Cyclomatic Complexity;
77 * ``L`` is the number of Source Lines of Code (SLOC);
78 * ``C`` is the percent of comment lines (important: converted to radians).
79
80 .. note:: Maintainability Index is still a very experimental metric, and
81 should not be taken into account as seriously as the other metrics.
82
83 Raw Metrics
84 -----------
85
86 The following are the definitions employed by Radon:
87
88 * **LOC**: The total number of lines of code. It does not necessarily
89 correspond to the number of lines in the file.
90 * **LLOC**: The number of logical lines of code. Every logical line of code
91 contains exactly one statement.
92 * **SLOC**: The number of source lines of code - not necessarily
93 corresponding to the **LLOC**.
94 * Comments: The number of comment lines. Multi-line strings are not counted
95 as comment since, to the Python interpreter, they are just strings.
96 * Multi: The number of lines which represent multi-line strings.
97 * Blanks: The number of blank lines (or whitespace-only ones).
98
99 The equation ``SLOC + Multi + Single comments + Blank = LOC`` should always
100 hold. Additionally, comment stats are calculated:
101
102 * `C % L`: the ratio between number of comment lines and **LOC**,
103 expressed as a percentage;
104 * `C % S`: the ratio between number of comment lines and **SLOC**,
105 expressed as a percentage;
106 * `C + M % L`: the ratio between number of comment and multiline strings
107 lines and **LOC**, expressed as a percentage.
108
109 Halstead Metrics
110 ----------------
111
112 Halstead's goal was to identify measurable properties of software, and the
113 relations between them. These numbers are statically computed from the source
114 code:
115
116 * :math:`\eta_1` = the number of distinct operators
117 * :math:`\eta_2` = the number of distinct operands
118 * :math:`N_1` = the total number of operators
119 * :math:`N_2` = the total number of operands
120
121 From these numbers several measures can be calculated:
122
123 * Program vocabulary: :math:`\eta = \eta_1 + \eta_2`
124 * Program length: :math:`N = N_1 + N_2`
125 * Calculated program length: :math:`\widehat{N} = \eta_1 \log_2 \eta_1 +
126 \eta_2 \log_2 \eta_2`
127 * Volume: :math:`V = N \log_2 \eta`
128 * Difficulty: :math:`D = \dfrac{\eta_1}{2} \cdot \dfrac{N_2}{\eta_2}`
129 * Effort: :math:`E = D \cdot V`
130 * Time required to program: :math:`T = \dfrac{E}{18}` seconds
131 * Number of delivered bugs: :math:`B = \dfrac{V}{3000}`.
132
133 Further Reading
134 ---------------
135
136 1. Paul Omand and Jack Hagemeister. “Metrics for assessing a software system’s
137 maintainability”. Proceedings International Conference on Software
138 Mainatenance (ICSM), 1992. (`doi <http://dx.doi.org/10.1109/ICSM.1992.242525>`_)
139
140 2. Don M. Coleman, Dan Ash, Bruce Lowther, Paul W. Oman. Using Metrics to
141 Evaluate Software System Maintainability. IEEE Computer 27(8), 1994. (`doi
142 <http://doi.ieeecomputersociety.org/10.1109/2.303623>`__, `postprint
143 <http://www.ecs.csun.edu/~rlingard/comp589/ColemanPaper.pdf>`_)
144
145 3. `Maintainability Index Range and Meaning
146 <http://blogs.msdn.com/b/codeanalysis/archive/2007/11/20/maintainability-index-range-and-meaning.aspx>`_.
147 Code Analysis Team Blog, blogs.msdn, 20 November 2007.
148
149 4. Arie van Deursen, `Think Twice Before Using the “Maintainability Index”
150 <http://avandeursen.com/2014/08/29/think-twice-before-using-the-maintainability-index/>`_.