Commenting the source code.

P

Profetas

Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.


Thanks Profetas
 
J

Joona I Palaste

Profetas said:
I know that this is off topic. but I didn't know
where to post.
Do you comment your source code while coding or after coding.
for example:
you write a procedure and after it is working, you'll comment it.

you write a line of code and a comment following.

I generally add a comment every time I finish a piece of code that does
some non-trivial task. I don't have any rules based on actual LOC
metrics but instead count the things that my code actually does, on a
semantic level. Sometimes I write several pieces of code into a single
source file, and only later comment them all, one by one.
 
D

dandelion

Profetas said:
Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

Sometimes i'll comment after coding.

Sometimes ill comment whilst coding.

Sometimes i'll comment before coding as a "pseudolanguage" to-do-list or
providing a nice general explaination of the algorithm(s) used, pitfalls and
solutions, quirks, oddities and general notes.

As long as the comments are there...
 
R

Rob Thorpe

Profetas said:
Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.


Thanks Profetas

When write a function I write comments above lines that I think need
them. Later I write a comment that describes the purpose of the whole
function, but I don't do that until after it's done and mostly
integrated because the purpose normally changes slightly when this
happens.
 
P

Profetas

Thanks for the reply.

My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.

If it is something complex.


Does anybody agree?

Profetas
 
T

Thomas Stegen

Profetas said:
Thanks for the reply.

My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.

If it is something complex.

Does anybody agree?

Not me. My two main points about comments.

1) If something low level needs a comment you are only solving the
symptom, not the problem. Is this absolute? No. Sometimes you need
obscure code for performance issues, though sometimes second guessing
the optimizer kills it. Keyword: Think.
2) Comment the abstraction a function is meant to represent, document
the pre and post conditions, arguments, range, domain and side
effects. Keywords: Detail without verbosity.


And then you have everything else to consider.
 
A

Alan Balmer

Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.
Before, during, and after :)

Sometimes I'll write a comment before a block of code to make sure I
have my thoughts in order and know where I'm going. I write comments
as needed during coding, and after coding, I review to see if there
are any clever things I've done in the heat of the moment that I won't
understand next week.
 
H

Herbert Rosenau

Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.

I follow these rules:
- design the whole program
- break down the design to be more specific
- break down each specific design to be more specific
repeat until each problem is broken into peaces lttle enough
to fill only a screen or a bit more
build modules holding a sub problem (of a sub problem....)
check anything. Recheck it again to be sure to have no mistake in
the design
- design the interfaces
even no coding - only design
recheck that them match any request, change if needed
- document anything well

- start filling modules with documented interfaces and empty function
definitions.
let any trunc return one error result (NULL pointer when it has to
return a pinter in result, anything else that presents an error in
other cases)
insert an debug functiion that says that the function is not
realised yet
This gives you the possibility to start
- build the headers and document them
- build the makefile and document it
test the makefile
- write, document, compile and debug debug fuctions.
You needs debugged debug functions to get your debug sessions more
successfully. Whereas successfull means dedect bugs, checks the
wanted results occure.

Now the project can change in its coding phase
- code, low level fuctions
- comment anything logically
that means write comments relayed to the logical funtionality
but NOT to the coding
- check the comment as if it were code for correctness in logic and
flow
correct errors now!
- when the comments are perfect fill each comment (section)
with the matching code. Use the makefile to to test for errors in
typing.
- write short testcase to debug each function. Neends no great
comments as they
are used only temporary to test the fuctions for korrectness
This ends up with well tested very, very low level fuctions. You
knows
that each function does anything as espected - including failture
handling.
- restart at top of this section but one logical level higher
while testing you can use the lower level functions without testing
them
because the prior cycle has done that careful, so bugs are only in
the new code.

Comments are essential for
- finding design bugs before the first code line gets written
- finding bugs while coding (that is why your comment is on logical,
not
coding level anyway
- finding bugs while debugging. Each programmer gets sometimes a bit
crazy
and writes code other than the design says you should.

True, yes, designing of the whole program and all its lower leveled
functions costs a lot of time. But at least spending one day for the
design and checking it saves you a full week and more on coding and a
month on debugging. So spend any time you needs to get your design
perfect. You saves a multiple time of that in coding.

Debug each little fuction extensive. It is your time you wins. Test
each function separately before you goes to the next one. That is
because you can use it later without doubt of an bug inside it. So you
gets higher level functions more quick ready for use.
 
E

Eric Sosman

Profetas said:
Thanks for the reply.

My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.

If it is something complex.

Does anybody agree?

Not I. If your understanding of what you are doing
is so tenuous that it can't survive the attempt to write
it down, the chances that your understanding is correct
to begin with are poor. Brian Kernighan says "Debugging
is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible,
you are, by definition, not smart enough to debug it.”
I take that as strong advice against writing code at the
limits of your comprehension.

Personally (and this is only a matter of taste), I
like to give each function a block comment describing its
purpose, and I write this comment before writing the code.
I also like to put line-by-line comments on important data
structures and variables, and I tend to write those as I
write the declarations. And that's about it: I'll sometimes
go back after the fact and add comments for anything that's
tricky or unclear or unusual, but since I try to follow
Kernighan's advice such comments are pretty rare.
 
J

John Bode

Profetas said:
Hi,

I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.


Thanks Profetas

Depends on what I'm writing and who I'm working for at the time. Some
companies have fairly strict documentation standards. Many don't.

If I'm creating a new file, there's usually a template that I work
from. I copy the template, modify the header as required, then start
writing code. When I'm creating a new procedure, I first create the
doc header for it (name, inputs, outputs, returns, etc., as dictated
by the standards of whomever I'm working for at the time), then write
the procedure body. Within the procedure body, I'll usually write all
the code without stopping, then go back and comment where necessary.
If I'm fixing a defect, I'll first insert a comment with the defect
tracking number, then add the fix, then go back and add to the comment
if necessary (although usually, details of the fix are documented in
the CM and defect tracking tools, not the source -- you can use the CM
tool to view the actual code changes).
 
M

Michael B Allen

Do you comment your source code while coding or after coding.

Aside from what other folks said I want to point out that many people,
including myself, greatly dislike huge block comments like:

/*************************************************************
* This is a HUGE stupid comment. Can you tell?
*
*************************************************************/

You might think this is obviously wrong but believe me when I tell you it
happends far too often. I purchased a book once called "Data Structures
and Algorithms in C" (or something like that) that has HUGE blocks of
comments that were crazier than the above. I felt pretty stupid afterward
for not looking closer at it.

As others have advocated I too much prefer seletive comments. A
particularly nice non-intrusive technique is to use something like the
below with little right-justified annotations to walk the reader through
the code:

....
if (c2 > c1) { /* append to end of list */
if (ISADJ(c1,c2)) { /* join with last cell */
c1->size += POFF + c2->size;
return 0;
}
c2->next = c1->next;
suba->tail = c1->next = ref;

return 0;
}

while (c1->next < ref) { /* find insertion point */
c1 = SADR(suba, c1->next);
}
c3 = SADR(suba, c1->next);

j1 = ISADJ(c1,c2); /* c1 and c2 need to be joined */
j2 = ISADJ(c2,c3); /* c2 and c3 need to be joined */

if (j1) {
if (j2) { /* splice all three cells together */
if (SREF(suba, c3) == suba->tail) {
suba->tail = SREF(suba, c1);
....

Mike
 
J

Jason Whitehurst

Profetas said:
My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.
If it is something complex.
Does anybody agree?

Completely disagree.

Commenting the line may help you to formulate your thoughts to understand
exactly what you are coding. You may find, while writing the comment, that
there is a much easier, more efficient, whatever way to code it - and not
waste time coding it twice.
 
N

Neil Kurzman

Profetas said:
Thanks for the reply.

My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.

If it is something complex.

Does anybody agree?

Profetas

No one really comments afterward. they just say they will, but there is
never time.
 
M

Malcolm

Profetas said:
My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.

If it is something complex.

Does anybody agree?
Yes. Comments are for people, including you after some time, who need to
look at the code after it is finished. If you put in comments during actual
writing you may need to take them out, you may find it hard to "comment out"
code, and, worst of all, you may invalidate a comment and forget to change
it, something no compiler can catch.
Also, as you say, writing comments may take your attention from the actual
code. People will differ in this, but certainly if someone has imposed a
very burdensome commenting regime on you then it may slow you down
considerably if you try comment as you code.
I am not absolutely systematic, but normally I write code in modules without
comments. Then I test it. When the module is "ready" I put in comments
saying what all the functions do and how to call them. If it is presentation
code I might also comment non-obvious lines (eg "BMP files begin with
letters BM", or "sign extend"), generally I also comment structure members.
 
S

Simon

Profetas said:
I know that this is off topic. but I didn't know
where to post.

Do you comment your source code while coding or after coding.

for example:

you write a procedure and after it is working, you'll comment it.

or

you write a line of code and a comment following.

Comments are for script-kiddy Visual BASIC programmers. Real men
don't comment code: if it was hard to write it should be hard to
understand.
 
J

Joona I Palaste

Malcolm said:
Yes. Comments are for people, including you after some time, who need to
look at the code after it is finished. If you put in comments during actual
writing you may need to take them out, you may find it hard to "comment out"
code, and, worst of all, you may invalidate a comment and forget to change
it, something no compiler can catch.

One of our source files over at work is rife with comments about some
obscure kludges that had to be inserted and need to be removed in the
future. But over the course of the program's development, no one has
bothered to find out what these kludges exactly are, which part of the
code they affect, and how (or if) they can be removed. So the file still
has comments about "This kludge should be removed by version n" when
we're already in version n+2. Actually the whole file needs complete
replacing but no one has got around to designing a replacement.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp
 
P

Peter Shaggy Haywood

Groovy hepcat Profetas was jivin' on Fri, 22 Oct 2004 06:23:28 -0400
in comp.lang.c.
Commenting the source code.'s a cool scene! Dig it!
I know that this is off topic. but I didn't know
where to post.

Do you not get comp.programming?
Do you comment your source code while coding or after coding.

Yes.
No, seriously, I usually write comments before, during and after I
write the code.
I often write comments before I start writing the code, to give me a
good idea of what the code should do. I make these comments clear and
concise. They clearly set out each step of the algorythm/procedure to
be used. Then I write code to implement those steps.
Of course, there are always times when you look at some code after
you've written it and think that a comment would help explain some
part of the code or the reason for some weird-looking workaround. You
may have thought it was obvious before, and changed your mind when you
had to re-read the code months later, or even minutes later.
The rest of the time, it just makes sense to comment as you go. You
write down what you're doing and why. You do it before you forget.
for example:
you write a procedure and after it is working, you'll comment it.

No. That's not the best way to go. By the time you've got something
working you may have forgotten how you got it working, all the things
you tried. Comment as you go. If you make changes that affect the
accuracy of the comments, change the comments as you change the code.
or
you write a line of code and a comment following.

Most of my comments precede the lines of code they explain. They
explain the intent of a group of logically linked statements. Some
comments, however, go at the ends of lines - short comments at the
ends of short lines. These are somewhat along the lines of labeling.
A comment for each and every line of code is excessive. You should
comment every group of lines, where a group performs some single part
or step of the procedure to be performed. Comment each step, not each
line. And even then, don't comment what is absoulutely, blatantly
obvious.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Groovy hepcat Profetas was jivin' on Fri, 22 Oct 2004 11:40:15 -0400
in comp.lang.c.
Re: Commenting the source code.'s a cool scene! Dig it!
My theory is that commenting whilst you are coding,
will break your concentration on what you were programming.
If it is something complex.
Does anybody agree?

Someone might, someplace in the World. But I don't.
Comments often help me concentrate on what I'm doing. Keep them
simple, short and to the point. Don't try to explain how a whole 500
line function works, in detail, in a single comment, otherwise you
will get distracted. But writing helpful comments along the way makes
coding easier.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
C

CBFalconer

Peter said:
.... snip ...

Comments often help me concentrate on what I'm doing. Keep them
simple, short and to the point. Don't try to explain how a whole
500 line function works, in detail, in a single comment,
otherwise you will get distracted. But writing helpful comments
along the way makes coding easier.

If you have a 500 line function the code is almost certainly
already beyond hope. The exception is probably a switch statement.
 
F

Flash Gordon

If you have a 500 line function the code is almost certainly
already beyond hope. The exception is probably a switch statement.

In general I would say that even if it is a switch statement it is
probably still difficult to read. I would generally write a function for
any case of significant length and call that function.

Of course, every rule has an exception including this one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top