Main Content

Language Scope

Cost of maintaining or changing a function

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 measures the cost of maintaining or changing a function. It is calculated as:

(N1 + N2)/(n1 + n2)
Where:

  • N1 is the number of occurrences of operators.

    Other than identifiers (variable or function names) and literal constants, everything else counts as operators.

  • N2 is the number of occurrences of operands.

  • n1 is the number of distinct operators.

  • n2 is the number of distinct operands.

    The metric considers a literal constant with a suffix as different from the constant without the suffix. For instance, 0 and 0U are considered different.

When reporting this metric, Polyspace® rounds the calculated language scope to the first decimal place. Because the intent of this metric is to indicate the maintainability of a function, language scope of functions defined within local classes are not computed.

Tip

To find N1 + N2, count the total number of tokens. To find n1 + n2, count the number of unique tokens.

The recommended upper limit for this metric is 4. For lower maintenance cost for a function, try to enforce an upper limit on this metric. For instance, if the same operand occurs many times, to change the operand name, you have to make many substitutions.

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

Examples

expand all

int g(int);
int f(int i)
{
    if (i == 1)
        return i;
    else
        return i * g(i-1);
}

In this example:

  • N1 = 19.

  • N2 = 9.

  • n1 = 12.

    The distinct operators are int, (, ), {, if, ==, return, else, *, -, ;, }.

  • n2 = 4.

    The distinct operands are f, i, 1 and g.

The language scope of f is (19 + 9) / (12 + 4) = 1.8.

namespace std {
  int func2() {
    return 123;
  }
};

namespace my_namespace {
  using namespace std;

  int func1(int a, int b) {
    return func2();
  }
};

In this example, the namespace std is implicitly associated with func2. The language scope computation treats func2() as std::func2(). Likewise, the computation treats func1() as my_namespace::func1().

For instance, the language scope value for func1 is 1.3. To break down this calculation:

  • N1 + N2 = 20.

  • n1 + n2 = 15.

    The distinct operators are int, ::, (, comma, ), {, return, ;, and }.

    The distinct operands are my_namespace, func1, a, b, std, and func2.

Metric Information

Group: Function
Acronym: VOCF
HIS Metric: Yes

Version History

expand all