...
- local variable declaration statements [Java 2005, §14.4]
- field declarations [Java 2005, §8.3]
- field (constant) declarations [Java 2005, §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; i
remains uninitialized.
Code Block | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
int i = 1; // purpose of i... int j = 1; // purpose of j... |
Compliant Solution (Initialization)
In this compliant solution, it is readily apparent that both i
and j
are initialized to 1.
...
Declaring each variable on a separate line is the prefered method. However, multiple variables on one line are acceptable when they are trivial temporary variables such as array indexes.
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 |
---|
// 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 | ||
---|---|---|
| ||
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; } } |
Exceptions
DCL01-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; it 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.
...
Code Block | ||
---|---|---|
| ||
public class Example { void function() { int mx = 100; // some 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 |
---|---|---|---|---|---|
DCL01-J | low | unlikely | low | P3 | L3 |
Related Guidelines
C Secure Coding Standard: "DCL04-C. Do not declare more than one variable per declaration"
C++ Secure Coding Standard: "DCL04-CPP. Do not declare more than one variable per declaration"
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2ea64d1690bf4acb-69687bf4-44aa41a3-9f0ea52d-79938eed7e02338859ed861d"><ac:plain-text-body><![CDATA[ | [[Conventions 2009 | AA. Bibliography#Conventions 09]] | Section 6.1, "Number Per Line" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="da955f1784854299-5145a8a2-486e47c6-81249534-896be4a665b9f74b6e358825"><ac:plain-text-body><![CDATA[ | [[ESA 2005 | AA. Bibliography#ESA 05]] | Rule 9: Put single variable definitions in separate lines. | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e96e27382aff290a-5646b4d5-4d59464f-bf83908d-4dd695f461f7707c84ba5146"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§8.3, "Field Declarations" | http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3] | ]]></ac:plain-text-body></ac:structured-macro> |
| |||||
| |||||
| |||||
|
...