Code metrics
-
Upload
tomek-rekawek -
Category
Technology
-
view
337 -
download
0
description
Transcript of Code metrics
© 2012 Cognifide Limited. In commercial confidence only.
Code quality
© 2012 Cognifide Limited. In commercial confidence only.
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.
Coding standards
© 2012 Cognifide Limited. In commercial confidence only.
Coding standards – exampleclass 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.
Cyclomatic complexity
© 2012 Cognifide Limited. In commercial confidence only.
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.
House of cards
© 2012 Cognifide Limited. In commercial confidence only.
House of cards
© 2012 Cognifide Limited. In commercial confidence only.
House of cards
© 2012 Cognifide Limited. In commercial confidence only.
Complexity for sequential codebyte[] 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.
Complexity with one conditionbyte[] 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.
Complexity: formal definitionFormal 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.
Class responsibilities
© 2012 Cognifide Limited. In commercial confidence only.
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.
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.
Coupling
© 2012 Cognifide Limited. In commercial confidence only.
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 batterypublic 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.
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.
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.
Technical debt
© 2012 Cognifide Limited. In commercial confidence only.
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.
Other metrics
© 2012 Cognifide Limited. In commercial confidence only.
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