It lets other know why that piece of code needs to be there in the first place.
While learning programming most of us forget how important it is to comment as you are writing code and over time we ignore it. At first, it might not seem to be a very important step, but as you start writing code that contains a lot of function, or while working on a project that contains several lines of code and multiple files, It gets harder to track why a certain block of code needs to be there. The code explains itself but the comment lets the reader know why it needs to be there.
You comment does not need to be a few paragraphs to explain what a specific block of code does, It can just be one line that lets you know what your code does.
Most of the programmers only comment large blocks of code to explain why it needs to be there, But over time it is important to known what those small blocks of code do.
It is very important to comment code that is being viewed by others, specially when the project is open source or when you are working in a team. A comment will be like a short explanation over what your code did overall and why it needs to be there, to another person who is reading the code.
I never gave commenting code importance because my code was too small or was never going to be read my someone else, and I thought that I will always remember what I have written, even if I look back at the code after several days or months, I was proved wrong, It became harder for me to understand why a certain block of code was needed in my project, when I read my code of my project after a few months. It took me a little time to understand why a certain block of code. I had written was there going there, It might be because, I am not a great programmer or maybe because I don’t code daily or have a coding job to do, but it was mainly because I never commented my code.
It is also a good practice when It comes to writing maintainable code.
To fix this problem, I decided that ever block of code I write, will have at least single line comment, after doing this it helped me understand what I had written and why I had written in over there, It helped me know what I did a few days or months back, especially when I had to refer to smaller blocks of code and implement them in my other projects.
Thanks for reading, feel free to leave a comment below.