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 can could cause confusion regarding about the types of the variables and their initial values. If more than one variable is declared in a declaration, care must be taken that the type and initialized value of the variable is known.
Noncompliant Code Example
In particular, do not declare any of the following in a single declaration:
- Variables of different types
- 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. While not required for conformance with this guideline, this practice is also recommended in the Code Conventions for the Java Programming Language, §6.1, "Number Per Line" [Conventions 2009].
This guideline applies to
- Local variable declaration statements [JLS 2013, §14.4]
- Field declarations [JLS 2013, §8.3]
- Field (constant) declarations [JLS 2013, §9.3]
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: In this noncompliant code example, a programmer or code reviewer might mistakenly believe that the two variables {{src}} and {{c}} are declared as {{int}}. In fact, {{src}} has a type of {{int \[\]}}, while {{c}} has a type of {{int}}. Wiki Markup
Code Block | ||
---|---|---|
| ||
int src[], ci, j = 1; |
Compliant Solution (Initialization)
In this compliant solution, each variable is declared on a separate line.it is readily apparent that both i
and j
are initialized to 1:
Code Block | ||
---|---|---|
| ||
int src[]i = 1; //* sourcePurpose array */of i... int c;j = 1; //* maxPurpose value */ |
Although this change has no effect on compilation, the programmer's intent is clearer.
Noncompliant Example
In this noncompliant example, a programmer or code reviewer might mistakenly believe that both i
and j
have been initialized to 1. In fact, only j
has been initialized, while i
remains uninitialized.
Code Block | ||
---|---|---|
| ||
int i, j = 1;of j... |
Compliant Solution (Initialization)
In this compliant solution, it is readily apparent that both i
and j
have been are initialized to 1.:
Code Block | ||
---|---|---|
| ||
int i = 1; int, 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 indices.
Noncompliant Code Example (Different Types)
In this noncompliant code example, the original programmer declared declares multiple variables, including an array, on the same line. Since even arrays All instances of the type T
have access to all Object
methods, mistakes of this form may not be immediately detected by the compiler or an IDEmethods 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 class ExampleExample<T> { private T a, b, c[], d; public Example(T in) { a = in; b = in; c = (T[]) new Object[10]; d = in; } } |
Thus, when it comes time to write something like the toString
methodWhen an Object
method, such as toString()
, is overridden, a programmer might accidentally write it without realizing c
is an array. Since the mistake compiles cleanly, it may go undetected.could accidentally provide an implementation for type T
that fails to consider that c
is an array of T
rather than a reference to an object of type T
.
Code Blocknoformat |
---|
public String toString() { return a.toString() + b.toString() + c.toString() + d.toString(); } |
However, the intended toString
might programmer's intent could have been to invoke toString
for each T
in ()
on each individual element of the array c
.
Code Block |
---|
// Correct functional implementation
|
No Format |
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:Move each declaration to a different line, so programmer error of thinking c
is a T
object, isn't as likely. Furthermore, declare arrays by putting the brackets adjacent to the type, as opposed to postfixed to the variable name.
Code Block | ||
---|---|---|
| ||
public class ExampleExample<T> { 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; } |
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-evident.
Declarations of loop indices should DCL04-01: Trivial declarations for loop counters, for example, can reasonably 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 | ||
---|---|---|
| ||
public class Example { void function() { int mx = 100; // Some max value for (int i = 0; i < mx; ++i ) { /* ... */ } |
Risk Assessment
Declaring no more than one variable per declaration can make code easier to read and eliminate confusion.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL04-C | low | unlikely | low | P3 | L3 |
Other Languages
This rule appears in the C Secure Coding Standard as DCL04-C. Do not declare more than one variable per declaration.
This rule appears in the C++ Secure Coding Standard as DCL04-CPP. Do not declare more than one variable per declaration.
References
}
}
|
Such declarations are not required to be in a separate line, and the explanatory comment may be omitted.
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.DCL52.MVOS | Do not declare multiple variables in one statement | ||||||
SonarQube |
| S1659 |
Bibliography
§6.1, "Number Per Line" | |
[ESA 2005] | Rule 9, Put Single Variable Definitions in Separate Lines |
[JLS 2013] | §4.3.2, "The |
...
\[[JLS 06|AA. Java References#JLS 06]\] Section 6.1, "Declarations"
\[[JLS 06|AA. Java References#JLS 06]\] Section 4.3.2, "The class Object" Wiki Markup