Professional Documents
Culture Documents
“If you cannot measure it, you cannot improve it”, Lord kelvin
Size matters
LoC: this is an example of
code complexity metric
Size matters
LoC: this is an example of
code complexity metric
• Halstead metrics
– Length: number of operators and operands
– Vocabulary: number of unique operators and unique operands
– Difficulty = (unique operators / 2) * (operands / unique operands)
– Volume: how much information the reader has to absorb to understand the code.
– Effort: amount of effort to rewrite a codebase (excluded all the work related, like understanding
specifications)
– Halstead Time: how much time is needed to rewrite a codebase.
Halstead metrics have been criticized for many reasons. Today people tend to think that these
metrics are not well adapted to modern languages…
Henrique M adeira Analysis of Software Artifacts, DEI-FCTUC, 2022/2023 7
Object-Oriented
Number of Classes
Number of Methods
Cohesion
Coupling
…
Henrique M adeira Analysis of Software Artifacts, DEI-FCTUC, 2022/2023 8
Code understandability
Understandability
• Measures
– The number of linearly independent paths through the control flow graph
(basic set)
• V(G) = E – N + 2
– E is the number of flow graph edges
– N is the number of nodes
Predicates:
Or • Nodes with multiple exit arcs.
• Corresponds to branch/conditional
• V(G) = P + 1 statement in program.
– P is the number of predicate nodes
Henrique Madeira,
Henrique IPN-LIS, February 1, 2023
M adeira Analysis of Software Artifacts, DEI-FCTUC, 2022/2023 10 10
10
min = A[0];
1 1
i = 1;
while (i < N) { 2
2 T
F
if (A[i] < min) 3
3
min = A[i]; 4
i = i + 1; 5 F T
} 5 4
print min; 6 6
Henrique Madeira,
Henrique IPN-LIS, February 1, 2023
M adeira Analysis of Software Artifacts, DEI-FCTUC, 2022/2023 11 11
11
min = A[0];
1 1
i = 1;
while (i < N) { 2
2 T
F
if (A[i] < min) 3
3
min = A[i]; 4
i = i + 1; 5 F T
} 5 4
print min; 6 6
V(G) = E – N + 2 = 7 – 6 + 2 = 3
V(G) = P + 1 = 2 + 1 = 3
Predicates: node 2 and node 3
12
min = A[0];
1 1
i = 1;
while (i < N) { 2
2 T
F
if (A[i] < min) 3
3
min = A[i]; 4
i = i + 1; 5 F T
} 5 4
print min; 6 6
13
• Test cases: 1
– P1 = (1, 2, 6) 2
F T
• Test Case: A = { 5 }, N = 1 3
Expected Output: 5
F T
– P2 = (1, 2, 3, 5, 2, 6) 5 4
• Test Case: A = { 5, 9 }, N = 2 6
Expected Output: 5
– P3 = (1, 2, 3, 4, 5, 2, 6)
• Test Case: A = { 8, 6 }, N = 2
Expected Output: 6
Define at least one test case for each linearly independent path
14
15
16
17
Experiment
27 programmers
8 proficient
19 ordinary
3 code comprehension
tasks
EEG, HRV, eye tracker Program
Expected
V(g) LoC
No.
Goal
NASA Task Load complexity units
18
19
The Spearman correlation coefficient (r s) calculated for the scores obtained for the mental effort (NASA TLX) and cognitive load
(EEG) for all participants in the three programs is r s = 0.829, with a corresponding p < 0.0001, which indicates a high correlation
between the cognitive load measured using EEG and the subjective perception of mental effort declared by the
participants
20
21
Cognitive load (EEG) can detect even small differences in the code complexity (that
have impact on the mental effort)
22
Variables seems to play an important role. Calls to library functions and external APIs is
relevant. The semantics of the algorithm is important.
23
CC-Sonar much lower than the value recommend as threshold for code refactoring (CC-
Sonar > 15) does not guarantee that programmers easily understand the code unit.
24
à CC Sonar > 15
25
0.90
Cognitive load (EEG) vs No. Revisits
0.80 Nrevisits
0.70
CL(E EG)
0.60
0.50
0.40
0.30
0.20
0.10
0.00
B
B
C .E
B
A
C C
C
D
C C
C .1
C .2
C C .3
C .1
C .2
C .3
C .1
C .2
C .3
C .4
C .5
C .6
C .7
C .8
C C .9
D
C .10
C .11
.12
A
1.
2.
3.
C
C
A
A
A
C
C
C
C
C
C
C
C
1.
1.
2.
2.
2.
3.
3.
BC
2
C
C
C
C
C
1.
C
C
C
C
2.
2.
2.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
3.
2.
26
1
Henrique M adeira Analysis of Software Artifacts, DEI-FCTUC, 2022/2023 27
27