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 declaration statement can cause confusion regarding the types of the variables and their initial values. If more than one variable is declared in a declaration, care must be taken to ensure that the type and initialized value of the variable is self evident.

...

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

...

In this noncompliant example, a programmer or code reviewer might mistakenly believe that both i and j have been are initialized to 1. In fact, only j has been is initialized, while i remains uninitialized.

...

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

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

...

In this noncompliant example, the original 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 that arrays need 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 {
  private T a,b,c[],d;

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

As a result, when a method of Object like toString() is overridden, a programmer might accidentally provide a general implementation for type T without realizing that c is an array.

...