Several variables declared in one statement
ID |
apex.maintainability.one_declaration_per_line |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Readability |
Language |
Apex |
Description
Reports a declaration statement that introduces more than one variable, with or without initializers.
Rationale
A combined declaration hides how many names a statement introduces and which of them are initialized: in Integer retries, attempts = 0; only the second gets a value, and the first stays null until something assigns it - a null that surfaces later as a dereference failure far from this line. Splitting the statement also keeps the diff of a later change to one variable, instead of touching a line that three other variables share.
The following code illustrates the pattern detected by this rule:
public class OneDeclaration {
public void run() {
// FLAGGED: Several variables declared in one statement
Integer retries, attempts;
// FLAGGED: Several variables declared in one statement
String first = 'a', second = 'b';
// FLAGGED: Several variables declared in one statement
Integer counted, total = 0;
System.debug(retries + attempts + counted + total + first + second);
Integer single = 0;
String only;
System.debug(single + only);
}
}