Method body is empty
ID |
apex.maintainability.empty_function_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Apex |
Description
Reports a method whose body contains no statements. Constructors, interface declarations, auto-implemented properties and the Database.Batchable finish hook, which the platform requires whether or not there is anything to do, are not reported.
Rationale
An empty method is a promise the class does not keep: callers invoke it expecting something to happen and nothing does. In practice it is either a stub that was never finished, a body that a refactor emptied and nobody deleted, or an override written to silence a requirement - and all three read the same from the call site. The caller has no way to tell, which is what makes it worth reporting rather than ignoring.
The following code illustrates the pattern detected by this rule:
public class EmptyBodies {
public EmptyBodies() {
}
// FLAGGED: Method body is empty
public void doNothing() {
}
// FLAGGED: Method body is empty
public static Integer stillATodo(Integer seed) { }
public String accountName { get; set; }
public Integer total { get; private set; }
global void finish(Database.BatchableContext bc) { }
public void doSomething() {
System.debug('work');
}
}
Remediation
Implement the body, or delete the method and its callers. An implementation that must exist and has nothing to do - an interface member this class does not need - is the one legitimate case; a comment inside the body is invisible to the matcher, so exclude that finding rather than expecting the comment to silence it.