Main Content

Function stress complexity exceeds threshold

The function stress complexity of a function is greater than the defined threshold

Since R2025a

Description

Polyspace® calculates the function stress complexity (FSC) using this equation:

FSC = Cyclomatic complexity ⋅ (Number of calling function ⋅ Number of called function)2

Polyspace reports a violation of this rule when the function stress complexity of a function is greater than the defined threshold. For details about how Polyspace calculates this metric, see Function Stress Complexity.

Polyspace uses a default threshold of 10000 unless you specify a different threshold. To specify a selection file where you can set the threshold, use the option Set checkers by file (-checkers-selection-file) or Checkers activation file (-checkers-activation-file).

When you import comments from previous analyses by using polyspace-comments-import, Polyspace copies any review information on the code metric Function Stress Complexity in the previous result to this checker in the current result. If the current result contains the same code metric, the review information is copied to the code metric as well.

Risk

Exceeding the function stress complexity threshold indicates that the complexity of the internal structure and external connections of a function is higher than the acceptable threshold. Such a function makes your code more difficult to maintain because:

  • The function is internally complex and difficult to update.

  • Any update to the function requires follow-up changes to many upstream and downstream functions.

Fix

To fix this violation:

  • Refactor the function to reduce its cyclomatic complexity. This can require splitting a function into multiple functions.

  • A high level of complexity of the external connections of the function can indicate an architectural issue in your code. Redesign and refactor your code to reduce interdependency and mutual coupling of functions.

Examples

expand all

In this example, the function handleRequest() performs one of several actions depending on the value of option. The internal complexity of the function and the large number of called functions result in a function stress complexity value of 648. If you set the threshold for this Guideline at 500, then Polyspace reports a violation.

#include <stdio.h>

// Function declarations
void readData();
void processData();
void saveData();
void logData();
void sendData();
void generateReport();
void backupData();
volatile int getValue();
void handleError();

void handleRequest() {  //Noncompliant
    volatile int option = getValue();


    if (option == 1) {
        readData();
    } else if (option == 2) {
        processData();
    } else if (option == 3) {
        saveData();
    } else if (option == 4) {
        logData();
    } else if (option == 5) {
        sendData();
    } else if (option == 6) {
        generateReport();
    } else if (option == 7) {
        backupData();
    } else {
        handleError();
    }
}

To set the threshold for this guideline to 500, save this XML code in a file and use the file as the input to the analysis option Checkers activation file (-checkers-activation-file):

<?xml version="1.0" encoding="UTF-8"?>
<!-- DO NOT EDIT THIS FILE: Some changes can lead to a crash of Polyspace products -->
<polyspace_checkers_selection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="selection_schema.xsd" revision="1.0">
  <standard name="Guidelines">
    <section name="Software Complexity">
      
      <check id="SC21" state="on">
        <threshold>500</threshold>
      </check>
    </section>
  </standard>
  
</polyspace_checkers_selection>

Correction — Refactor the Code

One possible correction is to refactor the code to reduce the internal complexity and the number of function calls in the function body. In this code, handleRequest() selects functions from an array of function pointers, which reduces the internal complexity. Because the functions are invoked using function pointers, the number of calls in the function body is also reduced, resulting in a function stress complexity value of 8, which is below the specified threshold 500.

#include <stdio.h>

// Function declarations
void readData();
void processData();
void saveData();
void logData();
void sendData();
void generateReport();
void backupData();
volatile int getValue();
void handleError();
// Array of function pointers to reduce complexity
void (*operations[])() = {
    readData, processData, saveData, logData, sendData, generateReport, backupData
};

void handleRequest() {
    int option = getValue();

    // Simplified control flow using function pointers
    if (option >= 1 && option <= 7) {
        operations[option - 1]();
    } else {
        handleError();
    }
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC21
Default Threshold: 10000

Version History

Introduced in R2025a