It's been observed that the performance of software applications starts degrading as the data volume and user base of the application increases. Hence, the need arises to identify the bottlenecks by capturing certain metrics, analyzing them against benchmarks and then applying techniques such as refactoring to optimize source code.
This article gives a brief introduction of the imperative of quality metrics and covers the usage of various tools that can be integrated with the development environment to automate the process of metrics-driven code analysis. This setup can then be applied to an application under maintenance or being developed afresh.
Software Quality Metrics
Software quality metrics captured during the design or build phase provide quantitative assessment of the source code quality and are good indicators of performance, maintainability, extensibility, reusability and testability of the application. Hence, it can be considered a data-driven approach to enhance code quality. Some code metrics are listed below for quick reference:
- Lines of Code (LOC) -- A measure of the number of code lines and also indicates size of a class.
- Comment Ratio (CR) -- Counts ratio of comments to total lines of code.
- Number of Attributes (NOA) -- Counts the number of attributes in a class.
- Cyclomatic Complexity (CC) -- Indicates the number of linearly independent paths through a method.
- Coupling Between Objects (CBO) -- Represents the number of other classes to which a class is coupled.
- Depth of Inheritance Hierarchy (DOIH) -- Counts how far down the inheritance hierarchy a class or interface is declared.
- Maximum Number of Parameters (MNOP) -- Counts the highest number of parameters defined for a single method, from among all the class methods.
- Maximum Number of Levels (MNOL) -- Counts the maximum depth of if, for and while branches in the bodies of methods.
- Number of Added Methods (NOAM) -- Number of methods added by class (compared to parent class)
- Weighted Methods per Class (WMC) -- Sum of the McCabe Cyclomatic Complexity for all methods in a class
- Response For Class (RFC) -- This includes methods in the class's inheritance hierarchy and methods that can be invoked on other objects. (Number of Local Methods + Number of Remote Methods)
- Number of Overridden Methods (NORM) -- Total number of methods in the selected scope that are overridden from an ancestor class
- Afferent Coupling (Ca) - The number of classes outside a package that depend on classes inside the package.
- Efferent Coupling (Ce) - The number of classes inside a package that depend on classes outside the package.
Implement Metrics in Java Projects
Every Java-based project must incorporate quality metrics in its project plan by defining metric goals and strategies. For each software metric being measured, a threshold value must be specified. This threshold can either be based on past projects of similar nature or on an organizational process database. Refer to Table 1 for the indicative threshold values of certain quality metrics.
Indicative threshhold for code metrics.
Identify the tool/plug-in that can help capture metrics during the build phase. Various factors such as ease of integration with IDE, in-built metrics defined, level of customization supported, etc., need to be explored during tool selection. The tool should be able to scan the source code and produce reports of structural fitness posture in terms of metrics.
Refer to Table 2 for the various tools/plug-in available, with supported features and limitations.
Tools/plugins to measure code metrics
A team orientation on quality metrics, available tools and associated benefits is the next step to ensure metrics get captured and enforced for every piece of code being added or modified.
As a final step, captured values need to be compared against threshold by reviewing reports generated by tool to track deviation and plan appropriate corrective action. Once necessary corrections are made in source code (such as refactoring), metrics should be captured again to validate no more deviation exists from threshold.
Qualitative and Quantitiative Benefits
A. Qualitative Benefits
* Code Metric tools easily get integrated with IDEs and helps developers perform static code analysis.
* Metrics give a measure of Code Quality and hence, code conforming to benchmarks is easier to maintain. Also good quality software ensures better performance in long run.
* Software code complexity remains under control and application is in compliance to certain best practices from industry.
B. Quantitative Benefits
* Helps reduce Cost of Quality by reduction in appraisal cost which in turn improves productivity and quality of application.
* Very less effort required to measure and analyze violations. Also, any team member can generate metrics reports.
* Makes code reviews effective wherein reviewer focuses more on business aspects while adherence to coding standards is taken care through automation. (Refer to Figure 1.)
Benefits of quality metrics in projects.
It has been observed that overall performance of a software application drastically improves if code metrics are captured timely and corrective measures are implemented. Overall, it can act as a sales differentiator from your competition.