Lecture 6 - Code Quality
Lecture 6 - Code Quality
Lecture 6
• Two levels:
– High level: code architecture
– Low level: specific code
Organization of software project
• Have a well-defined package/folder structure
in software project.
– Create a clear hierarchy of packages and resources
folders.
– Put relevant classes in the same package.
– Put relevant resources in the same folder.
– Separate resources from source codes, software
modules from components...
Managing relationships
among software components
• Should be small
• Should have single responsibility
• Should have small number of instance
variables
Coupling Between Object Classes
• The measure of how many other classes rely on the
class and vice versa.
– Two classes are considered coupled when methods
declared in one class use methods or instance variables of
the other class.
• Large values imply:
– More complexity, Reduced maintainability, Reduced
reusability
• What to do?
– Split the class into smaller ones if possible
– Mark as ‘highly dependent’, put more maintenance efforts
Depth of Inheritance Tree
• The number of inheritance layers that make
up a given class hierarchy.
• Large values imply more design complexity.
– Many object classes may have to be understood to
understand the object classes at the leaves of the tree.
• What to do?
– Consider carefully before an attempt to increase DIT.
Number of methods per class
• An indicator of the amount of effort required to
implement and test a class.
– High values suggest that the class is too large, difficult to
understand and maintain.
• Weighted methods per class
– Complex methods are given more weight.
• What to do?
– Split methods into smaller methods.
– Move methods into suitable classes.
– Split class into smaller classes.
Fan-in/Fan-out
• Fan-in of method X: the number of methods that call
method X.
– A high value means that changes to X will have extensive
effects on other parts of the program.
• Fan-out of method X: the number of methods that
are called by method X.
– A high value suggests that the overall complexity of X is
high.
Clean Code
• Suggestions:
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
PhoneNumber phoneString;
/**
* Returns the day of the month.
*
* @return the day of the month.
*/
public int getDayOfMonth() {
return dayOfMonth;
}
The importance of code formatting
• Keep each line not longer than screen width.
• Have blank lines to separate the package declaration,
imports, and each of the methods.
• Keep related lines of code close together.
public class FitNesseServer implements SocketServer {
private FitNesseContext context;
public FitNesseServer(FitNesseContext context) {
this.context = context; }
public void serve(Socket s) {
serve(s, 10000); }
public void serve(Socket s, long requestTimeout) {
try {
FitNesseExpediter sender = new FitNesseExpediter(s, context);
sender.setRequestParsingTimeLimit(requestTimeout);
sender.start(); }
catch(Exception e) {
e.printStackTrace(); }
} }
References
• Roger S. Pressman, Bruce R. Maxim, 2020.
Software Engineering - A practitioner’s
approach, 9th edition. McGraw Hill.
• Robert C. Martin, 2008. Clean Code - A
Handbook of Agile Software Craftsmanship.
Prentice Hall.