Slide 1

Source Code Metrics

300+ metrics providing objective measurement and
visualisation of non-functional qualities

Automated Source Code Metrics for C and C++

Cantata offers over 300 source code metrics for C/C++, providing an objective and useful measurement and visualization of the non-functional qualities of your source code. These metrics include:

Measuring non-functional qualities involves the static inspection of the source code, which assesses various non-functional features related to the software. This is invoked when building a Cantata-enabled software project. You can configure the analysis through the following methods:

  • Enabling/disabling system headers analysis
  • Specifying which static analysis metrics to calculate using an options file
  • Specifying the source files, functions, or classes to be analyzed
  • Specifying the precise source code statements to be analyzed using pragmas in the source code

By using Cantata, you can improve the quality of your C/C++ code, make it more maintainable, and enhance your development process. Try Cantata today and see how it can help your software project.

Code complexity and structure

Cantata offers support for measuring code complexity metrics on procedural source code. These metrics are recognized by academia and are common sense approaches to increase software maintainability through objective measurement. The supported metrics include:

  • Halstead’s Software Science metrics
  • McCabe’s, Myers’, and Hansen’s cyclomatic complexity metrics
  • Average and maximum nesting level
  • Basic counts of language constructs such as comments, lines of code, statements, and parameters.

By incorporating these metrics into the development process, Cantata can help developers identify potential problem areas in the codebase and make improvements to increase software quality and maintainability.

Object-oriented implementation

Cantata offers more than just code complexity measures for object-oriented code. It also provides a comprehensive set of source code metrics that measure different aspects of object-oriented implementation. These metrics include:

  • Chidamber and Kemerer’s MOOSE metric set.
  • Fernando Brito e Abreu’s MOOD metric set.
  • Bansiya and Davis’ QMOOD metric set.
    Robert Martin’s object-oriented dependency metrics.
  • McCabe’s object-oriented metrics.
  • Bansiya’s class entropy metrics.

We understand the importance of accurate and actionable source code metrics. That’s why all our metrics are provided at the function, class, translation unit, or system level, as appropriate. With Cantata’s source code metrics, you can gain insights into the quality and maintainability of your codebase and make data-driven decisions to improve your software development process.

Testing effort estimation

Understanding the complexity of source code is essential for estimating the time required for testing. Cantata’s source code metrics utilize industry-standard complexity measures to provide accurate estimations of testing efforts for source items. For example, McCabe Cyclomatic Complexity and its variants can be used to determine the minimum number of test cases needed to achieve complete decision code coverage.

Visualising and reporting metrics

Visualizing data through graphs can enhance understanding and provide an overall view of trends that may not be immediately apparent from numerical metrics alone. Graphs can be plotted at various levels, including class, function, or category. While formatted metrics are valuable, graphical representation can be even more useful for data analysis.

Example uses of metrics

As Cantata can produce over 300 static metrics on source code, below are some examples of specific metrics and their most useful application. For an exhaustive list please refer to the Cantata manual.

Standard Code Size Metrics

These are simple metrics regarding the number of lines of code, comments, etc.

Name Description Scope
LINE_CODE Total number of lines of code (including blank lines and comments). Function or system
LINE_COMMENT Total number of lines of comments (both C and C++). Function or system
LINE_SOURCE Total number of lines of source code (not including blank lines or comments). Function or system

Standard Code Quality Metrics

The quality of a piece of software is to some degree based on the number of occurrences of dubious code contained within it. These metrics alert the user of such occurrences.

Name Description Scope
LABEL_GOTOUSED Number of goto labels that are used. Function or system
LABEL_GOTOUNUSED Number of unused goto labels. Function or system
STMT_GOTO Number of goto statements. Function or system
SWITCH_NODEF Number of switch statements with no default. Function or system
SWITCH_FALLTHRU Number of non-empty case blocks which fall through to the next case block. Function or system
UNREACHABLE Number of statically unreachable statements in the given scope. Function or system

Standard Complexity Metrics

The complexity of a piece of code is generally regarded as a measure that will affect the effort involved with maintaining it. These metrics attempt to estimate the complexity of the software based on various factors, such as the level of nesting.

Name Description Scope
HALSTEAD_PARAMS Number of parameters. Function
MCCABE The McCabe Cyclomatic Complexity value for the function. Function
NESTING_MAX Maximum statement nesting level. Function
NESTING_SUM Sum of the statement nesting levels for all statements in the function. Function

Specialist Object Oriented Metrics

Many standard metrics are still applicable to OO systems. For example, the maximum nesting levels within functions is also applicable to class methods. However there are also a range of specific OO metrics. These may be with respect to a given class, or for the system as a whole.

Name Description Scope
MAX_DEPTH Maximum length of inheritance path to ultimate base class. System
MOOD_AD Number of new attributes defined for this class. Class
MOOD_MD Number of new methods plus overridden methods defined for this class. Class
MOOD_AHF Proportion of attributes that are hidden (private or protected). Class
MOOD_MHF Proportion of methods that are hidden (private or protected). Class
MOOSE_CBO Level of coupling between objects.  The number of classes with which this class is coupled (via a non-inheritance dependency from this class to that, or vice versa). System
MOOSE_WMC_MCCABE Average McCabe Cyclomatic Complexity value of for all methods of the class (excluding inherited methods) defined in this translation unit. Class
MOOSE_LCOM98 Chidamber & Kemerer’s Lack of Cohesion of Methods metric (1998 definition).  The minimum number of disjoint clusters of (new or overridden) methods (excluding constructors), where each cluster operates on disjoint set of (new) instance variables. Class
MOOSE_RFC Chidamber & Kemerer’s Response for a class metric.  The number of methods or functions defined in the class or called by methods of the class. Class

The ‘OO’ aspects of the C++ language have tended to render the old procedural C metrics less useful, but fortunately new sets of metrics have taken their place. The popular ones include MOOSE (Metrics for OO Software Engineering), MOOD (Metrics for OO Design), and QMOOD (Quality Metrics for OO Design). Between them they define a number of metrics which can be useful for judging whether a C++ class is ‘worth testing’. Some examples are:

Quality identified in source code EXAMPLE METRICS
Poor or Questionable Design

‘MCCABE Quality’

‘MOOSE Lack of Cohesion among Methods’

‘MOOD Attribute Hiding Factor’

Estimated Number of Faults

‘MOOD Methods Defined’

‘MOOD Attributes Defined’

‘MOOSE Weighted Methods in Class’

General Complexity

‘MOOSE Depth of Inheritance’

‘QMOOD Number of Ancestors’

‘MOOSE Number of Children’

Estimated Test Effort

‘Methods Available’

‘MOOSE Response for a Class’

‘MOOSE Coupling Between Objects’

‘MOOD Method Hiding Factor’

Additional System Metrics

Additional system level metrics can be created by taking averages for various class or function scope metrics. For example, we can calculate the mean McCabe Cyclomatic Complexity value for all functions or methods within our system.