Successfully reported this slideshow.
Upcoming SlideShare
×

# Code metrics

247 views

Published on

What are code metrics, why are they important, and how they used.

Published in: Data & Analytics
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Code metrics

1. 1. CODE METRICS Eng. Mohammad R. Katby
2. 2. Which line of code easier to understand? int x = x + 1; int x = y + 1; or CODE METRICS Eng. Mohammad R. Katby
3. 3. Agenda ■ What are the code measurement units? ■ What we are going to measure? ■ Why we need code metrics ■ What are code metrics
4. 4. What are Code Metrics ■ Length ■ Vocabulary ■ Difficulty ■ Volume ■ Effort ■ Bugs ■ Structuredness ■ Complexity ■ Maintainability ■ Independability
5. 5. 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
6. 6. Pioneers of Code Metrics Thomas J. McCabe - 1976 Maurice Halstead - 1977
7. 7. int x = x + 1; Operators Operands Operand: 4, Unique: 3 Operator: 3, Unique: 3 OD = 4; UOD = 3 OP = 3; UOP = 3
8. 8. 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;
9. 9. 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
10. 10. Halstead metrics “Derived” ■ Bugs (BUG) =VOL / 3000 = 18.06 / 3000 – Estimates how many bugs you are likely to find in the system.
11. 11. 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
12. 12. 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
13. 13. 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
14. 14. 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
15. 15. 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) ■
16. 16. Levels of Measurement ■ Method ■ Class ■ Package and system