Main Content

Compute Code Complexity Metrics Using Polyspace

This topic describes how to compute all code complexity metrics and then review the ones that exceed specified thresholds. To see metric values only if they exceed thresholds, check for guidelines on software complexity. See Reduce Software Complexity by Using Polyspace Checkers.

Code complexity metrics are a set of numbers that quantify the complexity of your C/C++ program. For instance:

  • A function with a high cyclomatic complexity contains too many branches.

  • A function with a high number of return statements has too many exit points.

Complex programs are difficult to debug, analyze, test and maintain. To avoid too much complexity, impose limits on the complexity metrics during coding.

Polyspace® does not compute code complexity metrics by default. To compute them during analysis, use the option Calculate code metrics (-code-metrics).

After analysis, the software displays project, file and function metrics on the Results List pane. You can compare the computed metric values against predefined limits. If a metric value exceeds limits, you can redesign your code to lower the metric value. For instance, if the number of called functions is high and several of those functions are always called together, you can write one function that fuses the bodies of those functions. Call that one function instead of the group of functions that are called together.

Impose Limits on Metrics (Desktop Products Only)

In the user interface of the Polyspace desktop products, open some results with metrics computations. Then impose limits on the metric values and update results on the Results List pane to show only metric values that exceed the limits.

  1. Select Tools > Preferences.

  2. On the Review Scope tab, do one of the following:

    • To use a predefined limit, select Include Quality Objectives Scopes.

      The Scope Name list shows the additional option HIS. The option HIS displays the HIS code metrics only. Select the option to see the limit values.

    • To define your own limits, select New. Save your limits file.

      On the left pane, select Code Metric. On the right, select a metric and specify a limit value for the metric. Other than Comment Density, limit values are upper limits.

      To select all metrics in a category such as Function Metrics, select the box next to the category name. For more information on the metrics categories, see Code Metrics. If only a some metrics in a category are selected, the check box next to the category name displays a symbol.

      The 'Code Metrics' section of a typical review scope. When the Status column is checked for a specific metric, it indicates that the metric is part of the review scope.

  3. Select Apply or OK.

    The drop-down list in the left of the Results List pane toolbar displays additional options.

    • If you use predefined limits, the option HIS appears. This option displays code metrics only.

    • If you define your own limits, the option corresponding to your limits file name appears.

  4. Select the option corresponding to the limits that you want. Only metric values that violate your limits remain on the Results List pane.

    These metrics are shown along with a red exclamation mark. For instance, the predefined scope, HIS, requires that every function should have only one return statement. If you select the scope HIS, you see the metric Number of return statements only if the number exceeds one.

    Code metrics that violate thresholds are shown with a red exclamation mark when you select the HIS scope.

  5. Review each violation and decide how to rework your code to avoid the violation.

Note

To enforce coding standards across your organization, share your limits file that you saved in XML format.

People in your organization can use the Open button on the Review Scope tab and navigate to the location of the XML file.

Impose Limits on Metrics (Server and Access products)

In the Polyspace Access web interface, limits on code complexity metrics are predefined. In the Dashboard perspective, if you select Code Metric, a Code Metrics window shows the metric values and limits.

To find the limits used, see HIS Code Complexity Metrics.

See also Code Metrics Dashboard in Polyspace Access Web Interface (Polyspace Access).

See Also

Related Topics