Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2022.2

Declaring multiple variables in a single declaration could cause confusion about the types of variables and their initial values. In particular, do not declare any of the following in a single declaration:

  • variables Variables of different types
  • a A mixture of initialized and uninitialized variables

In general, you should declare each variable on its own line with an explanatory comment regarding its role. Although it is While not required for conformance with this guideline, this practice is also recommended in the Code Conventions for the Java Programming Language, Section 6§6.1, "Number Per Line" [Conventions 2009].

When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evident.

This guideline applies to

Noncompliant Code Example (Initialization)

This noncompliant code example might lead a programmer or reviewer to mistakenly believe that both i and j are initialized to 1. In fact, only j is initialized; , while i remains uninitialized.:

Code Block
bgColor#FFcccc
int i, j = 1;

Compliant Solution (Initialization)

In this compliant solution, it is readily apparent that both i and j are initialized to 1.:

Code Block
bgColor#ccccff
int i = 1;  // purposePurpose of i...
int j = 1;  // purposePurpose of j...

Compliant Solution (Initialization)

In this compliant solution, it is readily apparent that both i and j are initialized to 1.:

Code Block
bgColor#ccccff
int i = 1, j = 1;

Declaring each variable on a separate line is the preferred method. However, multiple variables on one line are acceptable when they are trivial temporary variables such as array indexesindices.

Noncompliant Code Example (Different Types)

In this noncompliant code example, the programmer declares multiple variables, including an array, on the same line. All instances of the type T have access to methods of the Object class. However, it is easy to forget that arrays require special treatment when some of these methods are overridden.

...

Code Block
public String toString() {
  return a.toString() + b.toString() + 
         c.toString() + d.toString();
}

...

Code Block
// Correct functional implementation
public String toString(){
  String s = a.toString() + b.toString();
  for (int i = 0; i < c.length; i++){
    s += c[i].toString();
  }
  s += d.toString();
  return s;
}

Compliant Solution (Different Types)

This compliant solution places each declaration on its own line and uses the preferred notation for array declaration.:

Code Block
bgColor#ccccFF
public class ExampleExample<T> {
  private T a;   // purposePurpose of a...
  private T b;   // purposePurpose of b...
  private T[] c; // purposePurpose of c[]...
  private T d;   // purposePurpose of d...

  public Example(T in){
    a = in;
    b = in;
    c = (T[]) new Object[10];
    d = in;
  }
}

Exceptions

Applicability

Declaration of multiple variables per line can reduce code readability and lead to programmer confusion.

When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evidentDCL01-EX1: Note that the declaration of a loop counter in a for statement is in violation of this guideline because the declaration is not on its own line with an explanatory comment about the variable's role. However, declaration of loop indices in for statements is not only a common idiom but also provides the benefit of restricting the scope of the loop index to the for loop itself. This is a specific reason to relax this guideline.

Declarations of loop indices should be included within a for statement even when this results in variable declarations that lack a comment about the purpose of the variable:

Code Block
bgColor#ccccff
public class Example {
  void function() {
    int mx = 100; // someSome max value

    for (int i = 0; i < mx; ++i ) {
      /* ... */
    }

  }
}

Risk Assessment

Declaration of multiple variables per line can reduce code readability and lead to programmer confusion.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

DCL54-JG

low

unlikely

low

P3

L3

Related Guidelines

C Secure Coding Standard: DCL04-C. Do not declare more than one variable per declaration

...

Such declarations are not required to be in a separate line, and the explanatory comment may be omitted.

Automated Detection

ToolVersionCheckerDescription
Parasoft Jtest

Include Page
Parasoft_V
Parasoft_V

CERT.DCL52.MVOSDo not declare multiple variables in one statement
SonarQube
Include Page
SonarQube_V
SonarQube_V
S1659


Bibliography

 


...

DCL50-JG. Use visually distinct identifiers      01. Declarations and Initialization (DCL)      DCL59-JG. Avoid ambiguous overloading of varargs methodsImage Added Image Added Image Added