Code metrics
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Code metrics

on

  • 303 views

 

Statistics

Views

Total Views
303
Views on SlideShare
303
Embed Views
0

Actions

Likes
1
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Code metrics Presentation Transcript

  • 1. Code quality © 2012 Cognifide Limited. In commercial confidence only.
  • 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. Coding standards © 2012 Cognifide Limited. In commercial confidence only.
  • 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. Cyclomatic complexity © 2012 Cognifide Limited. In commercial confidence only.
  • 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. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 8. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 9. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 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. 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. 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. Class responsibilities © 2012 Cognifide Limited. In commercial confidence only.
  • 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. 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. Coupling © 2012 Cognifide Limited. In commercial confidence only.
  • 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. 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. 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. Technical debt © 2012 Cognifide Limited. In commercial confidence only.
  • 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. Other metrics © 2012 Cognifide Limited. In commercial confidence only.
  • 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.