if statement with an empty body
ID |
apex.correctness.empty_if_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Apex |
Rationale
An empty branch evaluates a condition and does nothing with the answer, which is never what the author meant. It is normally one of two mistakes: the body was moved out during a refactor and the if was left behind, or the logic was inverted and the real work now sits in an else that is missing. Either way the condition is still evaluated on every record, so the cost is paid and nothing is gained.
The following code illustrates the pattern detected by this rule:
public class EmptyIfs {
public void run(Account a, Boolean flag) {
// FLAGGED: if statement with an empty body
if (a.Name == null) {
}
// FLAGGED: if statement with an empty body
if (flag) { }
if (a.Name == null) {
a.addError(Label.Account_Name_Required);
}
if (!flag) {
System.debug('off');
}
}
}