Do your code comments suck? Avoid these 3 Traps!
Hey friend,
As a software engineer, you regularly work on projects with lots of moving pieces. Even if your codebase is small, it grows and grows as features are piled on, and it’s difficult over time to keep track of these of everything that’s going on. Nobody likes working on code that’s confusing & lacking documentation, which is why we’re going to have a nice heart-to-heart on commenting code.
By commenting your code well, you can make a positive impact on the products you work on and earn the respect and gratitude of your teammates for writing software that's easy to read and maintain.
Unfortunately, even the best of us can fall into several traps with comments that make our code puzzling and frustrating to our colleagues (and even our future selves 🤦♂️)
Today I want to talk about 3 traps with respect to code comments and what you can do to avoid them. Let’s discuss!
Now before we jump in make sure to grab this free guide I put together. IT's 6 best practices and tips that will help you put the pedal to the floor on your productivity and code quality. Commenting code is only 1 of these tips, and the other 5 are powerful strategies you can employ that will supercharge your ability to write code faster and cleaner, so stick around until the end!]
Alright, let’s dive in
Trap #1 – Overly Commented
Yes. You can actually add too many comments, and not all comments are useful! Let me explain.
Some questions that can lurk in the back of our minds when we write code comments are:
Have you ever come across code that literally had a comment before every line of code that follows? While not always the case, that’s usually a sign of something funky.
Sometimes in an effort to be helpful, we can unintentionally overexplain ourselves in comments.
Code that is overly commented:
Overly commented code might be better served in things like tutorials where the audience has a wide range of familiarity and expertise, and it makes sense to be as inclusive as possible.
Here are some general litmus tests for overly commented code:
[TOSS]
[Keep]
If your comment:
Keep It! Those are great things to have in comments
Trap #2 – Dead Code
We’ve all had (or will have) the story that goes like this:
“I just wrote a bunch of experimental code to help me solve my problem. It was helpful, but then something broke, so I commented the code out so I can use it for reference later on. Alright! I was able to replace my experimental code with something more robust and works way better! Now… what to do with this old function—should I delete it? Better keep it around just in case we need it in the future.”
Although well-intentioned, this hoarding mentality or sense of insecurity leads us to leave stuff like this in our codebases, cluttering them up for generations to come. If someone is curious about how something may have worked in the past, use a version control system with a blame function that lets them see how the code evolved over time.
A similar trap to this is forgetting to keep old comments up to date.
Did you change something in the code that makes the previous comment no longer relevant? Imagine how frustrating and confusing it would be for someone to read a comment about code that it wasn’t even intended for.
Delete unused code, and keep old comments up to date and you won’t fall into the trap of leaving dead code and comments in your codebase.
Trap #3 – Uncommented/Undercommented
Some people might throw around phrases like, “My code is self-documented” as a nebulous umbrella term to justify why their code has no comments.
“Self-documented” code, or code that is written clearly, concisely, with descriptive variable names etc. is absolutely something to strive for! But it doesn’t mean that the rest of your team (or even your future self) will have the context to understand your code.
Objection! I’m the only one who works on this code, no one needs to understand it but me!
Most of the time, you’re NOT the only one who needs to understand that code. But, even if that were true, your memory is not perfect. I guarantee you are not going to remember why you made every decision about why you wrote some code in a certain way for every single line of code you’ve written. Do your future self a favor, comment your code!
Here’s an analogy:
Imagine an engineer in the field disarming unexploded ordinances and landmines. Every once in a while he comes across a device and has to come up with an unorthodox solution using his wealth of experience and intuition. Perhaps he can safely remove the device from the field, but more work is needed to fully and properly disarm it. He may or may not be the person to follow up on this work which is why he provides brief, concise, clear context for the problem he encountered, what steps he took to resolve the issue, and what remains to be done.
Likewise, software is riddled with metaphorical landmines. When you come across them, the responsible thing to do is provide useful information or context so the code stays understandable and maintainable—helping the next person who comes along avoid getting blown up by something they weren’t expecting.
Here are some tips for improving your code that is lacking in comments
Wrapping up
OK – we covered 3 common traps that engineers fall into when commenting code. I hope this has been helpful to you, and you feel empowered to write clean, well-documented code that you can be proud of, and earns you respect and appreciation from your team. As a thank you for readying, I want to give you my 6 Tips for Writing Excellent Software. This no-fluff quick start guide that will help get results immediately. Go to www.sonicscholar.com/6tips and I’ll send it right to your inbox so you always have it.
Action Items
That's all for you now, I'll see you in another post soon. Happy coding (and commenting)!
50% Complete
We'll also send you more helpful content from time to time. No Spam! 100% Privacy Guarantee. Unsubscribe at any time.