I'm wondering whether it's necessary to add comments in my code. Sometimes, it feels like they just clutter things up and can be more irritating than helpful. Is this a common perspective? When working on a project alone, should I still include comments, or is it more important when collaborating with others?
4 Answers
Comments can seem like clutter, especially when they disrupt the flow of the code. If you do comment, make sure they explain *why* something is done rather than *what* it does—good code should be clear enough that the 'what' is obvious. It’s best to use descriptive variable names and avoid overly compact code that’s hard to read. This way, you minimize the need for comments, but a few key notes on the rationale can be super helpful, especially for complex logic that might confuse others later on.
Yes, you should comment your code. If you feel like comments are more trouble than they’re worth, then you’re probably commenting incorrectly. It’s vital to add comments that explain why you're doing something rather than just describing what the code does, because the code is primarily for humans to read! Clarity over clutter should be the goal, and good names and structured code can often reduce the need for comments. But, be cautious—outdated comments can lead to confusion, so keep them relevant and updated!
I like to keep my code readable, but I still find it valuable to add comments. You’d be surprised how quickly you can forget why you wrote something. I've often come back to my code a day later and needed to spend a lot of time figuring out what I was thinking. A couple of comments can save you a ton of time in the long run!
I rarely comment my code, solo or with a team. I do think it's important to explain things that aren't obvious, but ideally, well-written code should speak for itself. Like, instead of writing `int timestamp; // Time since login in milliseconds.` I’d suggest something clearer like `int time_since_login_ms;`
Totally agree! Writing comments that remain accurate is crucial, especially since code can evolve over time. You really do need to think about when and where those comments will be helpful in the future!