Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single statement can may cause confusion regarding the types of the variables and their initial values. If When more than one variable is declared in a declaration, care must be taken to ensure that the type and initialized initial value of the variable is are self evident.

Noncompliant Code Example

Wiki Markup
In this noncompliant code example, a programmer or code reviewer might mistakenly conceivebelieve that the two variables {{src}} and {{c}} are both declared as type {{int}}. In fact, {{src}} is of type {{int\[\]}}, while {{c}} has a type of {{int}}.

Code Block
bgColor#FFcccc
int src[], c;

Another fallout of this example is that it This example declares the array in a largely an antiquated and unpopular style, with the brackets appearing after the variable name as in type name[]. In practice, arrays are typically . Arrays should be declared as type[] name, for improved clarity.

Compliant Solution

In this compliant solution, each variable is declared on a separate line. It also uses the preferable , using the preferred style for declaring arrays.

...

Although this change has no effect on compilation, it clarifies the programmer's intent is clearer.

Noncompliant Code Example

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

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

...

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

Noncompliant Code Example

In this noncompliant code example , the programmer declared multiple variables, including an array, on the same line. All instances of the type T have access to methods of the class Object. However, it is easy to miss forget that arrays need require special treatment when some of these methods are overridden. Oversights of this genre typically go undetected by compilers and IDEs, alike.

Code Block
bgColor#FFcccc
public class Example<T> {
  private T a, b, c[], d;

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

As When a result, when a method of Object like toString() is overridden, a programmer might accidentally provide a general an implementation for type T without realizing that fails to account for the fact that c is an array of T rather than a reference to an object of type T.

Code Block
// The oversight error leads to an incorrect implementation
public String toString(){
  return a.toString() + b.toString() + c.toString() + d.toString();
}

However, the real intent might have been to invoke toString() on each individual member element of the type T, in array c.

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

To be compliant, move each declaration to a different line. Furthermore, declare arrays by placing the brackets adjacent to the type, as opposed to using the postfix notationThis compliant solution places each declaration on its own line as well as using the preferred notation for array declaration.

Code Block
bgColor#ccccFF
public class Example {
  private T a;   // purpose of a...
  private T b;   // purpose of b...
  private T[] c; // purpose of c[]...
  private T d;   // purpose of d...

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

...

DCL04-01: Note that the declaration of a loop counter in a for statement is in violation of this recommendation, because the declaration is not on its own line with an explanatory comment about the role of the variable. However, the intent declaration of the loop counter is clear enough from the context in which it is declared to typically not require a comment about the role of the variable.loop indices in for statements is not only a very common idiom; it also provides the benefit of restricting the scope of the loop index to that of the for loop itself. These are sufficient reasons to relax this guideline in this specific case.

Declarations of loop indices should Trivial declarations for loop counters can reasonably be included within a for statement:

Code Block
bgColor#ccccff
for (int i = 0; i < mx; ++i ) {
  /* ... */
}

Risk Assessment

Failing to declare no Declaration of more than one variable per declaration can affect line may reduce code readability and cause misinterpretationslead to programmer confusion.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

DCL01-J

low

unlikely

low

P3

L3

...

Bibliography

Wiki Markup
\[[JLSConventions 20052009|AA. Bibliography#JLSBibliography#Conventions 0509]\] Section 6.1, "Declarations", Section 4.3.2, "The class Object"Number Per Line
\[[ESA 2005|AA. Bibliography#ESA 05]\] Rule 9: Put single variable definitions in separate lines.
\[[ConventionsJLS 20092005|AA. Bibliography#ConventionsBibliography#JLS 0905]\] Section 6.1 Number Per Line, "Declarations", Section 4.3.2, "The class Object"

...

DCL00-J. Use visually distinct identifiers      03. Declarations and Initialization (DCL)      DCL02-J. Use meaningful symbolic constants to represent literal values in program logic