6. Why we need measurements?
■ Without measurement you cannot understand code faults
■ See whether improvement can be made or not
■ Simply you can’t be Electrician if you can’t measure the voltage
7. Pioneers of Code Metrics
Thomas J. McCabe - 1976 Maurice Halstead - 1977
8. int x = x + 1;
Operators
Operands
Operand: 4, Unique: 3
Operator: 3, Unique: 3
OD = 4; UOD = 3
OP = 3; UOP = 3
9. Halstead metrics “Primitives”
■ Length (LTH) = OP + OD = 3 + 4 = 7
■ Vocabulary (VOC) = UOP + UOD = 3 + 3 = 6
■ Difficulty (DIF) = (UOP / 2) * (OD / UOD) = (3 / 2) * (4 / 3) = 1.99
■ OP = 3, UOP = 3, OD = 4, UOD = 4
■ LTH = 3 + 4 = 7
■ VOC = 3 + 4 = 7
■ DIF = (3 / 2) * (4 / 4) = 1.5
int x = x + 1;
int x = y + 1;
10. Halstead metrics “Derived”
■ Volume (VOL) = LTH * Log2(VOC) = 7 * 2.58 = 18.06
– You can view this as the ‘bulk’ of the code
– how much information does the reader of the code have to absorb to understand
its meaning
– biggest influence on theVolume metric is the Halstead length
11. Halstead metrics “Derived”
■ Bugs (BUG) =VOL / 3000 = 18.06 / 3000
– Estimates how many bugs you are likely to find in the system.
12. Halstead metrics “Derived”
■ Effort (EFF) = DIF *VOL = 1.99 * 18.06 = 38.96 / 18 = 2.16 (sec)
– The amount of mental effort required to recreate the software
13. Cyclomatic Complexity
■ Counting the number of decisions made in our source code.
■ Determine number test cases that need to be written
■ there is no exact limit that fits all organizations. However, a limit
of 10 is a good for such our industry
■ Measure: Strcuturedness, Complexity
14. Class Coupling
■ Measure how many classes a single class uses
■ Has been shown to be an accurate predictor of software failure
■ High coupling indicates design that is difficult to reuse and maintain
■ There is no limit that fits all organizations. However, a limit of 9 is optimal
■ Measure: Reusability, Maintainability,Changeability
15. Depth of Inheritance
■ The maximum length from the node to the root of the tree
■ Harder to predict behavior
■ Increase complexity of design since more classes and methods are involved
■ Have a greater potential for reusing
■ Several researches suggest that a DIT around 5 or 6 should be an upper limit
16. Maintainability Index
■ High value means better maintainability.
– LOW - red rating is a rating between 0 and 9
– MODERIATE - yellow rating is between 10 and 19
– GOOD - green rating is between 20 and 100
■ Maintainability Index = MAX(0,(171 – 5.2 * log(HalsteadVolume) – 0.23 * (Cyclomatic
Complexity) – 16.2 * log(Lines of Code))*100 / 171)
■