[comp.std.c] C needs reliable comments

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) (01/15/91)

The discussion of // comments started me thinking.  Did you realize
that C is one of the few programming languages, perhaps the only one,
in which you can never be sure of correctly commenting stuff out?

(a) You can use /* and */ to comment out a block of code.  Then
anywhere in that block, if a */ occurs, the comment terminates
prematurely.

(b) You can use #if 0 and #endif to comment out a block of code.  Then
if you have an invalid token or unbalanced quotes between these, the
"commented out" block of code, the compiler may abort compilation.

(c) You can use a non-C compiler and use // comments.  But if the
compiler isn't pure non-C, your comments may become invalid if a line
ends with a blackslash.  And you still can't use // to comment out a
block of code without editing each line.

C badly needs a way of commenting out a block easily.  It needs
something like this:

#comment  /* begins in column 1; everything following is a comment */
... stuff to be commented out ...
... nac esu #tnemmoc ot peek logla sersu yppah ...
#endc    /* first occurrence of #endc in column one ends #comment block */

The #comment...#endc blocks do not nest, and are required to be in
column 1, thus avoiding all necessity for parsing tokens.  What's nice
about this scheme is that, in a pinch, you could always used sed or an
equivalent editor to strip out comments if your compiler didn't support
them:

    #! /bin/sh
    # strip out #comment comments
    TMP=/tmp/$$
    for file
    do
       sed -e '/^#comment/,/^#endc/d' < "$file" > $TMP && mv $TMP "$file"
    done

So nobody will ever be completely helpless due to lack of downward
compatibility.
--
History never         |   Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com>
becomes obsolete.     |   UUCP:  oliveb!cirrusl!dhesi

guido@cwi.nl (Guido van Rossum) (01/17/91)

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:

>Did you realize
>that C is one of the few programming languages, perhaps the only one,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>in which you can never be sure of correctly commenting stuff out?

Oh?  I think you are being facetious.

Do comments in Pascal nest *according to the standard*?  I don't think so.

In standard Algol-60 the only form of comment begins (or should I use
past tense, 'began'?) with the keyword 'comment' and ends (ended) at the
first semicolon.  Pretty hard to comment out things there, too.

Algol-68 uses the same token to begin and to end comments.  It has three
or four variants ('comment', 'co' and #) but you'll have to scan the
stuff you want to comment out to be sure that there aren't occurrences
of the token you choose.

In fact in any language where comments nest you have to worry about
comment tokens occurring inside strings, unles it has a very weird
lexical structure.

--Guido (I'm sure I must have missed a smiley face somewhere...)

exspes@gdr.bath.ac.uk (P E Smee) (01/18/91)

In article <14884@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <2893@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>>(b) You can use #if 0 and #endif to comment out a block of code.  Then
>>if you have an invalid token or unbalanced quotes between these, the
>>"commented out" block of code, the compiler may abort compilation.
>
>This is fallacious.  If you start with valid code and "comment out" a
>section to leave valid code using #if 0 and #endif, there is no problem.
>If you wish to adopt the thesis that random character strings should be
>accepted as valid C program source code, you're even nuttier than we've
>come to expect from you.

Naw, I think he's proposing the hypothesis that there ought to be a way
for the programmer to assert 'this block is a comment, and it's none of
the compiler's business what is inside it'.  I've always wanted that,
as well.  It can be useful.  Particularly as one common reason for
wanting to comment blocks out is so you can begin debugging the bits
of a program which you think you have finished, while you're still
thinking about the bits that ought to be where you put the comment.

On the other hand, there IS a problem with his idea, still, which I've
pointed out in a response to his message.

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132

exspes@gdr.bath.ac.uk (P E Smee) (01/18/91)

In article <2893@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>The discussion of // comments started me thinking.  Did you realize
>that C is one of the few programming languages, perhaps the only one,
>in which you can never be sure of correctly commenting stuff out?

Actually, there are lots of them.

>(a) You can use /* and */ to comment out a block of code.  Then
>anywhere in that block, if a */ occurs, the comment terminates
>prematurely.
>
>C badly needs a way of commenting out a block easily.  It needs
>something like this:
>
>#comment  /* begins in column 1; everything following is a comment */
>... stuff to be commented out ...
>... nac esu #tnemmoc ot peek logla sersu yppah ...
>#endc    /* first occurrence of #endc in column one ends #comment block */
>
>The #comment...#endc blocks do not nest, and are required to be in
>column 1, thus avoiding all necessity for parsing tokens.  

No, this proposal is totally isomorphic with /*-*/ comments.  All you
have done is replace the 'meta-operator' /* with <newline>#comment, and
replace */ with <newline>#endc.  If you try to #comment out a block of
code which already contains a #-comment block, everything falls apart.
(IMHO) you MUST invent some form of nesting comment in order to get the
effect you want.  That has its own set of difficulties, which is why it
is such an uncommon feature.

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132

karl@ima.isc.com (Karl Heuer) (01/19/91)

In article <2893@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>Did you realize that C is one of the few programming languages, perhaps the
>only one, in which you can never be sure of correctly commenting stuff out?

Nonsense.  It seems to me that any language with delimited-pair comments has
the same property.

>(b) You can use #if 0 and #endif to comment out a block of code.  Then
>[it breaks] if you have an invalid token or unbalanced quotes between these

I agree with Doug that this is a silly objection.  But if we accept it...

>[proposal for a #comment...#endc block that is basically #if 0...#endif]

Objection (b) still applies.  The following two lines
	char *s = "continued\
	#endc /*"; /* the string ends with a slash-star */
are a valid C fragment, but if you comment them out, you get something which
is indistinguishable from an attempt to comment out the incomplete fragment
consisting only of the first line.

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (01/19/91)

In article <1991Jan18.100448.17770@gdr.bath.ac.uk> P.Smee@bristol.ac.uk (Paul Smee) writes:
> (IMHO) you MUST invent some form of nesting comment in order to get the
> effect you want.  That has its own set of difficulties, which is why it
> is such an uncommon feature.

// solves the problem without the difficulties. If you apply // to an
already commented-out section of code, you get the same commented-out
section of code.

---Dan

davel@cai.uucp (David W. Lauderback) (01/20/91)

In article <2893@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>C badly needs a way of commenting out a block easily.  It needs
>something like this:
>
>#comment  /* begins in column 1; everything following is a comment */
>... stuff to be commented out ...
>... nac esu #tnemmoc ot peek logla sersu yppah ...
>#endc    /* first occurrence of #endc in column one ends #comment block */
>
>The #comment...#endc blocks do not nest, and are required to be in
>column 1, thus avoiding all necessity for parsing tokens.  What's nice

But if they don't nest, you've defeated the purpose because there may be
a small section you've already commented out and now want to comment out
a much larger area (for a while).  Should I have to remove the #endc that
already exists?
-- 
David W. Lauderback (a.k.a. uunet!cai!davel)
Century Analysis Incorporated
Disclaimer: Any relationship between my opinions and my employer's
	    opinions is purely accidental.

dave@cs.arizona.edu (Dave P. Schaumann) (01/24/91)

In article <2915@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
|I wrote (among other things):
|
|   (b) You can use #if 0 and #endif to comment out a block of code.
|   Then if you have an invalid token or unbalanced quotes between
|   these, the "commented out" block of code, the compiler may abort
|   compilation.
|
|In <14884@smoke.brl.mil| gwyn@smoke.brl.mil (Doug Gwyn) responds:
|
||This is fallacious.  If you start with valid code and "comment out" a
||section to leave valid code using #if 0 and #endif, there is no problem.
||If you wish to adopt the thesis that random character strings should be
||accepted as valid C program source code, you're [name-calling deleted].
|
|If you have C code and isn't working right, you may want to temporarily
|comment it out and replace it with a working stub.

No.  Not comment out.  I would use the conditional compilation operators,
though.

|Furthermore, the term "code" includes not only the C part but also any
|other text that is part of a C source file but is not intended for the
|compiler.

Maybe for you.  The way I (and obviously ANSI) see it, is that code is code,
and comments are comments, and never the twain shall meet.  Comments can
be *any* text (except, of course, the comment-end delimiter.  If you want
to include something that is a comment, use /* */.

If you want to include something that is *code* (ie at least well formed
tokens), use the #if/#endif operators.  I think that ANSI is well within
the bounds of reasonableness to require that everything within the
conditional compilation operators be well formed tokens.  After all, it
is supposed to be code -- something that (perhaps, with a little work) someday
will be compiled.

| [example of comment/conditional compilation use deleted]

C is certainly not perfect.  No language is.  Comments cannot include random
text, and it is certainly reasonable that article headers and signatures
might contain "*/".  Personnaly, I strip off the non-code portions in this
case, and save them to a seperate file.

This is my final word on this thread: (I promise :)
	-comments are for hiding non-program text from the compiler.
	-the conditional compilation directives are for hiding code
	 from the compiler.

If you try to use one for the other's job, *you will have problems*.

|Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com|
|UUCP:  oliveb!cirrusl!dhesi


Dave Schaumann		|  And then -- what then?  Then, future...
dave@cs.arizona.edu	|  		-Weather Report