PHP In Action writes on the use of comments in code, specifically citing Eli White’s Commenting on Commenting over at PHP Advent. They are critical of Eli’s advice, saying that comments should be unnecessary, and that code should be clean enough to easily understand it.
There’s a lot of good advice, especially about writing clean code. But the article fails to address a good number of really critical details and to some degree misses the point of Eli’s article.
- Writing comments beforehand can help articulate the why/how/what of an application. I’ve written comments many times that spell out the logic step by step. This invaluable process helps a developer think (and rethink) the business logic, and often refactor the code in ways that speed up its execution or development, before any code is actually written.
- Descriptive, important comments make editing code easier. I’ve edited code where a big “Hey! Don’t Touch This!” would have been useful to me. Descriptive function names are helpful, but sometimes you can’t describe exactly what something does with a function name. Comments are helpful.
- Getting in the habit of writing comments helps create to-do items. If you’re in the habit of writing comments already you’re more likely to leave a “todo” item behind so the next developer can clean something up. You’ll leave helpful notes on your thinking, and maybe even spark an idea for re-factoring, something that “descriptive code” can’t do.
- Leaving good comments helps other members of your team understand your level of understanding, helping them help you improve those skills. Part of working on a team is improving each others skills and helping each other and a big part of that is knowing where each team member is. For the same reason that regular code check-ins and code review should be part of the development process, leaving notes that indicate where you struggled should be part of your process.
PHP in Action points out that writing good, self-descriptive code is important. I do not dispute that. Writing clear, concise code is important, and not filling in too many comments is critical. But self-documentation can only carry you so far; there has to be something else sometimes. That’s why PHP has comments at all.
Frustrated with your company’s development practices?
You don't have to be!
No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.
Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."