Code metrics

630 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
630
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Code metrics

  1. 1. Code quality © 2012 Cognifide Limited. In commercial confidence only.
  2. 2. What is code quality • Sample code quality questions: − Are methods and classes clean, readable and short? − Are connections between classes well-designed? − Does your code follow Java conventions?  private static final rather than private final static − Do you have unit tests? What is the coverage? − Don’t you have some other code-smells?  Eg. if statement with too many || conditions • Some issues caused by low quality code: − − − − Problems with adding new features Problems with collaboration Hidden bugs Hard to debug • Static code analysis tools can check all things automatically • Sonar – popular tool for Java © 2012 Cognifide Limited. In commercial confidence only.
  3. 3. Coding standards © 2012 Cognifide Limited. In commercial confidence only.
  4. 4. Coding standards – example class FileUtils { public final static String pngExtension = "png"; public static String getExtension(String filename) { int DOTPos = filename.indexOf("."); if (filename != null && DOTPos > -1) { String extension = filename.substring(DOTPos+1); return extension; } return null; } } © 2012 Cognifide Limited. In commercial confidence only.
  5. 5. Cyclomatic complexity © 2012 Cognifide Limited. In commercial confidence only.
  6. 6. Cyclomatic complexity • • • • • Basic code metric Describes number of “branches” in method or class Sequential code (no ifs, fors and whiles): complexity = 1 Each additional condition: +1 If method is too complex, we should split it into smaller ones © 2012 Cognifide Limited. In commercial confidence only.
  7. 7. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  8. 8. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  9. 9. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  10. 10. Complexity for sequential code byte[] buffer = new byte[1024]; InputStream stream = new FileInputStream("/my/file"); InputStream buffered = new BufferedInputStream(stream); buffered.read(buffer); buffered.close(); • Complexity: 1 • No conditions, simple flow © 2012 Cognifide Limited. In commercial confidence only.
  11. 11. Complexity with one condition byte[] buffer = new byte[1024]; File file = new File("/my/file"); if (!file.exists()) { return; } InputStream stream = new FileInputStream(file); InputStream buffered = new BufferedInputStream(stream); buffered.read(buffer); buffered.close(); • Complexity: 2 © 2012 Cognifide Limited. In commercial confidence only.
  12. 12. Complexity: formal definition Formal definition: Edges – Nodes + 2 Components for(int i=0;i<3;i++) { System.out.println(i); } if(j == 42) { System.out.println(”j is the answer”) } Edges: 9, Nodes: 8, Components: 1 E-N+2P=3 © 2012 Cognifide Limited. In commercial confidence only.
  13. 13. Class responsibilities © 2012 Cognifide Limited. In commercial confidence only.
  14. 14. Single responsibility principle • Each class should be responsible for only one thing • Example: class Driver: − 2 fields: car, brain, − 5 methods: drive(), goTo(), stop(), getAngry(), drinkCoffee(), • This class has 3 responsibilities: − Driving car − Getting angry − Drinking coffe • It should be split into 3 classes © 2012 Cognifide Limited. In commercial confidence only.
  15. 15. LCOM4 & cohesion • LCOM4 – Lack Of Cohesion Methods. • How many responsibilities class? • LCOM4 = 3 means that − there are 3 not connected sets of methods/properties − class can be split into three new classes. • Class should be designed to have LCOM4 = 1 • Low LCOM4 = high cohesion • Cohesion relates to coupling © 2012 Cognifide Limited. In commercial confidence only.
  16. 16. Coupling © 2012 Cognifide Limited. In commercial confidence only.
  17. 17. Tight coupling • If a class (eg. Driver) invokes methods on some other class (eg. Car) it’s tightly coupled with it • In this case we can’t easily change Car implementation to something other (eg. Tank) • Real-life example: iPhone and its battery public class Driver { private final Car car; public Driver(Car car) { this.car = car; } public void driveCar() { car.startEngine(); car.drive(); } public class Car { public void startEngine() { … } public void drive() { … } } } © 2012 Cognifide Limited. In commercial confidence only.
  18. 18. Loose coupling • Class doesn’t need to know about other classes existence • It only need to know about contracts (interfaces) provided by other class • Real-life example: screw and screwdriver public class Driver { private Vehicle vehicle; public Driver(Vehicle vehicle) { this.vehicle = vehicle; } public void drive() { vehicle.startEngine(); vehicle.drive(); } public interface Vehicle { void startEngine(); void drive(); } public class Car implements Vehicle { public void startEngine() { … } public void drive() { … } } } © 2012 Cognifide Limited. In commercial confidence only.
  19. 19. Coupling & cohesion – summary • Classes must be loosely coupled and highly cohesive. • Cohesion is the degree to which the methods of a single class are tight together. − If class is not cohesive (eg. two methods doesn’t share any property), it can be split to two classes. • Coupling is the degree to which each class is tight to the others. − Any change you make in one class has impact to some other classes. − Use interfaces & depdency injection • Read more… • Read even more… © 2012 Cognifide Limited. In commercial confidence only.
  20. 20. Technical debt © 2012 Cognifide Limited. In commercial confidence only.
  21. 21. Technical debt • „Doing things the quick and dirty way sets us up with a technical debt, which is similar to a financial debt.” • Metric based on: − − − − − • • • • duplications, violations, (lack of) comments, coverage, complexity. Absolute debt in mandays and cash Debt ratio – dependent on the project size. Useful for project manager Read more... © 2012 Cognifide Limited. In commercial confidence only.
  22. 22. Other metrics © 2012 Cognifide Limited. In commercial confidence only.
  23. 23. Other code metrics • • • • • Lines of code Unit test coverage Responses for class … All these metrics can be checked via Sonar − Free & very useful software − Can be integrated with Eclipse • Visual Studio provides integrated metrics as well © 2012 Cognifide Limited. In commercial confidence only.

×