Commented-out Apex statement
ID |
apex.maintainability.commented_out_code |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Apex |
Description
Reports a line comment that holds an Apex statement rather than an explanation - a declaration with an initializer, an assignment, a DML or debug call, a control-flow header or a return - recognised by the shape of the statement, not by a keyword alone.
Rationale
Commented-out code decays the moment the surrounding code changes: it keeps naming fields, methods and objects that may no longer exist, and a reader cannot tell whether it is a deliberate switch, an abandoned experiment, or the correct version that someone disabled during an incident. The org’s version control already holds every earlier revision, so keeping the old lines in the file buys nothing and costs a question at every review.
The following code illustrates the pattern detected by this rule:
public class CommentedOut {
public void run(List<Account> accts) {
// FLAGGED: Commented-out Apex statement
// System.debug('left over');
// FLAGGED: Commented-out Apex statement
// insert accts;
// FLAGGED: Commented-out Apex statement
//Integer retries = 3;
// FLAGGED: Commented-out Apex statement
// for (Account a : accts) {
// FLAGGED: Commented-out Apex statement
// return a.Name;
System.debug(accts.size());
// insert the records once the approval step is done
// returns the list of accounts the caller may update
// this method is called from the AccountTrigger handler
// delete the staging rows before the reload runs;
// Integer overflow is not possible here, the source caps at 1000;
// String comparison in Apex is case-insensitive for Id fields;
// if (the record is locked) the update below is skipped;
// return values are documented in the wiki;
}
}