More often than not, I have made the mistake of writing code sans a comment to go along with it. “What’s the point?” I ignorantly thought. “I know what the code i $s doing anyways, I won’t need a reminder.”
However, the truth my past self-failed to see was that you don’t always remember what your code is saying or doing.
Comments are a significant part of code writing. They provide an explanation to what you have written and sometimes additional information about how the line of code works or what the expected result is.
They are especially necessary when you’re in the process of writing a long program. In these cases, it can be easy to forget what a specific function did or why you had a certain line of code written in the first place. In addition to that, there may be times where you run across an error in your code. Having comments by each line of code can assist in finding where the error is or give a hint to where you might need to change your code.
Additionally, comments help outside readers understand what your code is saying and facilitate understanding what you’re trying to accomplish. Without the use of comments, it could be hard to look at another person’s code and recognize what they’re trying to do at first glance. Comments can also help you keep track of what changes you’ve made to your code.
The possibilities are endless with comments. They are important to proper and readable code writing and can be a super helpful tool.
Here are some tips to writing comments for any future and current code writers:
- Comments should not duplicate code! If you’re having trouble figuring out what to comment on about a certain line, think about why you need it, what you’re trying to achieve by writing it, or what it’s doing in that specific line.
- Comment on classes and functions! Classes and functions are a great place to start making comments. Include brief descriptions about the code that way you and others can look back on it and understand the purpose.
- Use clear language! Generally, you should avoid using slang, complex language, and personal remarks or jokes. However, if it is your own program for fun and not some professional piece, I’d say it’s okay to personalize it to whatever will make you understand it better! But something important to remember is that LESS IS MORE.
- Write comments at the same time you write your code! Depending on how long it takes for you to complete your code, during that duration, it’s possible that you could forget the purpose of your code and why you decided to write it. I strongly suggest writing your comments simultaneously with your lines of code to avoid any confusion.
Begin implementing comments into your lines of code to strengthen your skills and understanding!
Kindly,
By Kadence Ly.