Importance of comments

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Importance of comments

Post by Jackolantern »

(I highly suggest any and all novice developers to read this entire post)

I just wanted to make a thread about something I have been seeing a lot of lately. Many devs appear to not be making any kind of comments at all in their code. I know that some user's first exposure to coding has been Halls' tutorial series, but it is important to remember that the videos were Halls' comments. And if you are simply remaking Halls' tutorial series exactly as it works in the videos, it is not likely as important to add comments since the videos still exist to refresh you on how something works, and people can help you here with your code because they are likely also familiar with the same code.

However, if you are writing custom code or tweaking the tutorial code in any way, you really, really, really should be commenting it. I can't stress that enough. From what I have seen, a lack of commenting is the #1 reason novice programmer's projects become unmanageable and must be rewritten or scrapped. What seems crystal clear today will look like gibberish in a few weeks to a month or so later. Here are some simple guidelines to help you know when you need to be commenting clearly and often:

You probably don't need to make many comments if:
1. A script is very short
2. A script only does one obvious task
3. Through your function names and variable names the code reads as plain English (be careful with this one; what seems obvious now may not be obvious later. I suggest having a non-coder friend look at it and see if they could tell what the script does). These will usually also fall into another of these categories as well because self-documenting code usually has to be short and simple.

You probably do need to make many comments if:
1. The script is long
2. The script is procedural (if you don't know what this means, then it likely is; for PHP, it basically means the code is not object-oriented, and does not use classes as a source of organization)
3. The script does more than 1 thing
4. The script pulls data from other sources, such as a database

And remember: Comment NOW, as you write your code! You may tell yourself you will come back and do it, but no one ever does after the script is written. It is a best practice of programming, and a rule that has withstood the test of time over decades of coding.

As a quick tutorial section, here are the 2 main types of comments you are most likely to run across:

Code: Select all

// Single line comment. The 2 slashes in front make everything that follows on the same line a comment

/* A milti-line comment. A slash with a asterisk opens a comment that 
can spread over
multiple lines. This type of comment is also helpful for "commenting-out" code when you are debugging or when you rewrite a section of code.
The comment must be ended with the reverse of the opening: */
echo("We are now back in live code");
As an example of how I comment, you can check the code for my PBBG World Builder application. Look at the third post in that thread (worldbuilder.php) and see how much it is commented, and what the comments are addressing. You will also see that I have commented out (but left in) some experimental code for others or myself to tinker with later. The other scripts in this project that are not as commented all fall into one of the categories above, mostly that they are simple and do only one thing.

I have to say that now that I look back at the worldbuilder.php file, without reading comments it is totally incomprehensible to me now. Without the comments, I would likely have to study the code for hours to understand it, if I ever could. Without comments, if I wanted to change things later I may have to have rewritten the entire script, which wastes time and effort.

Commenting also greatly enhances our ability to help you with your code files. PHP code is usually spread out among many different files, and one or two scripts taken out of context may basically render us unable to help you with anything beyond simple syntax errors. We just can't tell what the script is doing if it is not very simple and very clear.

So, there you have it :) I have laid out, in my typical verbose manner, the reasons why commenting is critical to the success of any programming project.
The indelible lord of tl;dr
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Importance of comments

Post by SpiritWebb »

I would have to agree, this I have learned the hard way!!
Image

Image
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Importance of comments

Post by Callan S. »

Now I even tend to write comments before I write code. Often something like '// Have an ammo check here', or something right above where I'm going to code it. I actually find it more fluid these days to write comments first, then follow with code afterward. It helps the process of going from the imagination/inspiration stage to the actual making a physical thing/actual written code stage.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Importance of comments

Post by Jackolantern »

Callen, you should see if your IDE or editor works specifically with TODO comments, because that is exactly what you are explaining. I know Visual Studio does, I think maybe Eclipse does, and I believe Netbeans does, too. In an IDE that does support it, any comments that are written beginning with //TODO: will be put into a special TODO interface panel that can help you manage the things that are still on the plate to finish in your project.

This feature can be hugely helpful, and it would augment the process you are already using and bring it to the next level :)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Importance of comments

Post by hallsofvallhalla »

oh i love this thread. I am bad about not commenting everything then coming back to my code and getting lost. Especially when someone asks me about a game I released here and I am like, uh I don't remember why I did that. :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Importance of comments

Post by Jackolantern »

It can be hard to stay motivated to put in comments lol. When I was making my World Builder app, I had to keep telling myself "other people are going to see this, other people are going to see this" lol
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Importance of comments

Post by Callan S. »

Jack, I was mostly thinking of PHP and I'm using context for that. I'll have to check it out, because that really would be handy, thanks! :)
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Importance of comments

Post by Jackolantern »

Well Eclipse and Netbeans are both for PHP, too ;) And it is definitely a great feature!

EDIT: Here is a StackOverflow post about using TODO annotations in Eclipse. And here is one of the main pages for the PHP tools for Eclipse. It is a great multi-language IDE, and for that I think it is worth getting used to. There is very little you can't do in this IDE. I have my own little problems with it, but once you learn it, you have learned it and have an IDE for about 10+ languages, and that makes it worth it. I have not actually used ConText, but have heard it is a good one too.
The indelible lord of tl;dr
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: Importance of comments

Post by 62896dude »

I am SO guilty of not commenting. I am trying to fix it, but I just find it inconvenient, I have to keep telling myself that I will be glad I did it in the long run (which I know is true)
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Importance of comments

Post by Jackolantern »

Absolutely! It is either you convince yourself it will be better for you in the long-run, or you eventually get hit with one of those "I learned the hard way" situations so many experienced coders will tell you about ;) It definitely isn't optional!
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”