Main Content

Number of Call Levels

Depth of nesting of control flow statements

Description

Note

Use Bug Finder instead of Code Prover for computing code metrics. Support for computing code metrics in Code Prover will be removed in a future release. See Version History.

This metric specifies the maximum nesting depth of control flow statements such as if, switch, for, or while in a function. Polyspace® starts the count of nesting depth at 0 and each additional nesting level increments this metric. A function without control-flow statements has a call level 0.

You can start counting call levels from 1 instead of 0 by using the new command line option -start-level-from-one.

The recommended upper limit for this metric is 4. For better readability of your code, try to enforce an upper limit for this metric.

To enforce limits on metrics, see Compute Code Complexity Metrics Using Polyspace.

Examples

expand all

int foo(int x,int y)
{
    int flag = 0;
    if (x <= 0)
        /* Call level 1*/
        flag = 1;
    else
    {
        if (x <= y )
            /* Call level 2*/
            flag = 1;
        else
            flag = -1;
    }
    return flag;
}

In this example, the number of call levels of foo is 2. Polyspace considers else-if statements as nested if statements. For example, consider this code:

int func(int x, int y)
{
	int flag = 0;
	if(x <= 0) {
		/* Call level 1*/
		flag = 1;
	} else
		if(x <= y) {
			/* Call level 2*/
			flag = 1;
		} else
			if(x == y) {
				/* Call level 3*/
				flag = 1;
			} else {
				flag = -1;
			}
	return flag;
}
Here, the call level of func is 3.

int foo(int x,int y, int bound)
{
    int count = 0;
    if (x <= y)
        /* Call level 1*/
        count = 1;
    else
        while(x>y) {
            /* Call level 2*/
            x--;
            if(count< bound) {
                /* Call level 3*/
                count++;
            }
        }
    return count;
}

In this example, the number of call levels of foo is 3.

Metric Information

Group: Function
Acronym: LEVEL
HIS Metric: Yes

Version History

expand all