scitools.com

← Metric catalog

Unused Variables is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Unused Variables

API ID: CountUnusedVariable
Languages: Any
Targets: Functions

The number of local variables a function never reads

A local variable is counted when nothing in the project reads its value. Declaring, initializing and assigning a variable are all writes, so a variable that is only ever written counts as unused. Writing through a pointer reads the pointer, so p->field = 0 counts as a read of p. Parameters are not counted, because an unused parameter is usually required by the signature.

Example:

    int example()               // CountUnusedVariable = 2
    {
      int used = 1;             // read by the return statement
      int declaredOnly;         // +1 never mentioned again
      int writtenOnly = 2;      // +1 initialized, never read
      return used;
    }

    int counter()               // CountUnusedVariable = 0
    {
      int i;
      int sum = 0;
      for (i = 0; i < 10; i++) // both variables are read
        sum += i;
      return sum;
    }