Misguided advice has led many developers to treat comments as a code smell. But in Apex, where business logic often responds to opaque platform constraints, comments can be essential if used properly.
The idea that comments lie stems from the fact that compilers ignore them. If the code changes but the comment doesn’t, it becomes misleading. But so can method names, variable names, and even class structures. Consider this example:
apex
void deleteOpportunity(Account caseRecord) { insert new Contact(LastName = 'FirstName');}
Everything here is a lie, yet there are no comments. A misleading method name or parameter can be just as deceptive. The real issue isn’t comments. It's developers.
Use comments to explain design decisions, especially when they involve trade-offs or platform quirks. This is your best chance to help future readers understand not just what the code does, but why it does it that way.
apex
// We avoid re-querying already seen dependencies to prevent infinite loopsif (!alreadyQueried) { nextLevelIds.add(dep.id);}
These comments capture context that's hard to infer from code alone.
When you finally decipher a confusing block of code, leave behind the insight you just gained. Comments like these save time and reduce onboarding pain.
apex
// Aha! This handles circular references by marking nodes as repeated
Comments Don’t Lie, Developers Do
Misguided advice has led many developers to treat comments as a code smell. But in Apex, where business logic often responds to opaque platform constraints, comments can be essential if used properly.
All Code Can Lie
The idea that comments lie stems from the fact that compilers ignore them. If the code changes but the comment doesn’t, it becomes misleading. But so can method names, variable names, and even class structures. Consider this example:
Everything here is a lie, yet there are no comments. A misleading method name or parameter can be just as deceptive. The real issue isn’t comments. It's developers.
Comments That Add Value
1. "Why" and "Why Not" Comments
Use comments to explain design decisions, especially when they involve trade-offs or platform quirks. This is your best chance to help future readers understand not just what the code does, but why it does it that way.
These comments capture context that's hard to infer from code alone.
2. "Aha!" Comments
When you finally decipher a confusing block of code, leave behind the insight you just gained. Comments like these save time and reduce onboarding pain.
3. Reference Comments
Pointing to external documentation can clarify non-obvious constraints.
4. To-do Comments
Useful for temporary workarounds or deferred improvements. They’re better than nothing, especially if no ticketing system is used for minor tasks.
Comments to Avoid
Version Control Comments
Commenting change history in code is a poor substitute for Git. These comments bloat your code and become stale fast.
Use version control tools instead. If you're stuck without them, keep these comments lean and temporary.
ApexDoc When It Adds Nothing
ApexDoc-style comments are helpful in public APIs or open-source libraries. But when they merely restate the method signature, they just add noise.
Skip these unless your audience really needs the extra scaffolding.
Dead Code Comments
Commented-out blocks of code are dead weight. Delete them. Use Git for recovery, or use feature flags to safely deploy incomplete features.
If you’re afraid to delete code, the real issue is lack of trust in your tools.
What else could you learn?
This principle is based on Chapter 5 of the Clean Apex Code book.
The full chapter includes more examples of good and bad comments.