Coding is similar to writing prose. Every individual does it a little differently, and as a result, when our code is read, we all have a distinct voice. Different naming conventions and problem-solving logic exist in all of us. We all think our code makes sense — particularly if it works — but it’s possible that it doesn’t to someone else. To combat this, we all need to get better at code commenting. That way, whoever comes next to the project will have a clear path to understanding and improving/fixing our code.
To begin with, let’s make sure that we’re all on the same page regarding what comments are. In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters. The syntax of comments in various programming languages varies considerably.
Programs must be written for people to read, and only incidentally for machines to execute.
Do you comment the code you write? If you’re like most people, the answer to that question is “No.” But you should! Commenting your code can be helpful in more ways than you might expect.
Here are three reasons why it’s good to comment your code even when you’re studying:
1. Explaining what the code does will help you remember what it does.
Using comments to describe the code in your own terms, similar to taking notes or rubber-duck debugging, helps you remember how it works. It may also draw attention to problem areas. If you can’t understand how your code works or can’t come up with a helpful comment, that’s a sign you need to go over that concept again.
2. It’ll help you pick up where you left off.
Most of us have lives and careers that make it impossible to study full time. If you comment your code while you study, you can check the comments the next time you log in to get a short recap of what you just learned. Since those comments will be your own personal explanations written in your own words, this method of reviewing the code will most likely be easier and more effective than attempting to re-read the lesson to catch up.
3. It’s best to form good habits.
Probably nobody else is going to look at the code you write while you’re working through a personal project. You may not even look at it again. But even if that’s the case, it’s still a good idea to get in the habit of commenting your code. Developing the habit now could save you a lot of headaches later on!
comment your code the next time you work on a project. It could help you learn more effectively, and even if it doesn’t, you’ll be building good habits for the future. What are your tips and tricks for getting the most out of commenting your code?