Do you write comments in your code? If you do, that’s awesome, and if you don’t, start adding comments right away.
Remember the piece of code you thought was self-explanatory and didn’t need any comments? Now go back to that same piece of code and see if it is self-explanatory. Can you return to the same code and modify it at the same pace?
I couldn’t do it all until the code was straightforward boilerplate code or some standard stuff that you write every day. But if it is a complex logic, some condition or group of conditions, then it takes a lot of time to get back on track and start updating/modifying the logic.
But when I add comments, it doesn’t mean I can start immediately, but it takes a lot less time to get started.
The biggest advantage of writing comments is that you don’t need to go through each line of code to understand it first. You can directly reference it from the comment, and it becomes super easy to explain the code and underlying logic.
What to write in the comments?
I believe better comments are an equally important topic to discuss because bad comments make it harder to understand what is happening.
What would be a bad comment?
Again, it’s something that you think is obvious and you don’t need to include it in a comment. This may seem obvious to you, but it’s not the same as other people reading your code. This makes your comments less relevant.
So back to the topic: What to write in the comments?
Try to answer these two questions: What and Why? What are you currently doing, what is your current task called? Why are you adding this logic? What is the problem, and what is your solution to it?
And finally, try to make it useful not just to you but to everyone else who may be working on the same code in the future.
Do share your experiences when comments in code have made your life easier.