Jump to content

Why are comments important?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
Since comments don't do anything for the code, what's the purpose of them? I know all languages have them, and sometimes when I view the source code of things I see them, but they don't really say anything useful. I think worrying about them is a waste of time.

#2
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
Comments are very important because if you look at your code a month after you create it you'll have no idea what you were doing and why you did it. With comments you don't have this problem. Also, comments are good if you are sharing work with another programmer.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If the code is self-explanatory, there is no need for comments. Comments are useful for when you are doing something where either the algorithm or the technique is unclear. They are also useful in documenting specifications for functions or classes that someone else will be using. A common thing in programming is to have one person write a library for someone else, and only distribute the headers and compiled library. The person using the library can't see how things work, so must rely on the headers and comments to know what is legal and what isn't for that library.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
smith

smith

    Programmer

  • Members
  • PipPipPipPip
  • 153 posts

WingedPanther said:

If the code is self-explanatory, there is no need for comments. Comments are useful for when you are doing something where either the algorithm or the technique is unclear. They are also useful in documenting specifications for functions or classes that someone else will be using. A common thing in programming is to have one person write a library for someone else, and only distribute the headers and compiled library. The person using the library can't see how things work, so must rely on the headers and comments to know what is legal and what isn't for that library.


With that being said, the person using the library couldn't use it unless there were comments. A good programmer always uses comments.

for (int i;;) {

   cout << "Smith";

}


#5
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
Alright, I understand. I'm sure it is especially important if you aren't the only person working on the code, because comments could also be used to tell your partner what to do.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's also useful when you pick up code after working on other things for 6 months. There's nothing like looking at code that you KNOW made perfect sense when you wrote it and scratching your head.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
Just a reminder: Comments aren't only for explaining the code (the how) but also for explaining why the code does what it does (the why).

If you're code isn't ridiculously complicated, it can be read and understood what it's doing...but without a comment, there's no way to know *why* it's doing it.

#8
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
This is more of a topic for programming theory rather than language specific like Java.


food for thought.

#9
Guest_ShortCircuit_*

Guest_ShortCircuit_*
  • Guests
icepack is right. Comments are important in ALL coding, whether its HTML, Java, C++ etc - you should comment in everything you do.

#10
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
Well, I don't think they are very important in HTML as it is a very easy language to just look and see what is going on.

#11
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
I've coded in HTML a lot before and don't even know how to make comments there. In Java it's //, right (or \\ maybe), and I think in HTML it might be <!-- Hey --!> or something like that.