Nested Comments (long summary)

Martin Weitzel martin at mwtech.UUCP
Thu Feb 22 01:35:22 AEST 1990


To early for a summary? May be, but I don't hope so.

>From time to time between C programmers there starts a war between
two groups: One favours nested comments (I'll call them the 'NESTERs')
and the other doesn't like this at all (I'll call them 'PURISTs').

Though I'm using C for such a long time, that I myself am a little
biased and feel a more as a PURIST, I'll try to be fair to the NESTERs
in this summary.

I think, the discussion centers around the following issues:

1) Give the programmer the possibility to 'comment out' some
   code, especially during phases of debugging and testing.
2) Avoid the possibility of introducing 'subtle bugs'.
3) Care about efficiency of the Compiler when parsing
   (or here better "lexing") some source file.
4) Problems with rules for exact semantics of nested of comments.

Let's look who is better armed for the battle. (I'll not say, that this
group will win, as the 'Holy Scriptures'(%) allready have spoken truth.)

(%: Hi Henry, I hope you have no exclusive rights on this phrase :-))

IMHO (1) is the true reason why many NESTERs become so angry and
furious, for C invalidates one of their debugging techniques: Commenting
out cumbersome parts of the source. Of course, the PURISTs say: You
should not want to do this, rather use #if-#endif for that purpose.

To be fair, having #if-#endif would not speak *against* allowing
nested comments, so it's no real argument *for* the PURISTs, but at
least the NESTERs should answer the question: "Why do you still want
to use comments for this purpose, if you can do it in another way
equally well?".

If a programmer shifts from any-language-with-nested-comments to C,
he or she has to change a lot of habbits. Why not simply change the
way you use to 'comment out' parts of your source? You even gain
some advantage: Now you may switch "from outside" (with cc -D ...)
between your versions, esp. if you use #if a little systematically.

On the next round (2) I think, the NESTERs have a little more
weight on their side, because the subtle bugs they have shown
(mistakenly typing * / instead of */) seem a little more probable
and far harder to detect.

A PURIST (as me) must have a tool available, to check for this
(eg I have a pretty printer for C that prefixes every line in an
open comment with an asterix), but I confess, this is a workaround
and it would not guarantee, that I detect such bugs immediatly.
Of course, a compiler could choose to issue a warning, if a '/*'
is detected within a comment - but if the compiler cares at all,
why not make nested comments completly legal?

Let's go on to (3). I suppose a Compiler has to do a lot of work
anyway. If a comment is encountered (nesting allowed or not)
it must be read through and the sequence '/*' within a comment
seems far less probable than all the other characters. So, does
the time for counting levels of nesting really count? Come on all
you PURISTs (and me too): Don't beat onto the NESTERs with the
argument of "lost efficiency", this beats back onto you!

At this time it looks good for the NESTERs, but we still have (4).
I think Raymond Chen (raymond at math.berkeley.edu), has made a very
valid point in his article, from which the following is an excerpt:

  Please present a coherent rule for nested comments for which the
  following lines of code produce "expected" results:

	int openquote = 34; /* " */ int closequote = 34; /* Also " */
	int quote = 34; /* for a good time, type printf("*/ %c",quote) */
	/* int doublequote = 34; /* " */ */ /* don't need this one: '"' */
	/* printf("*/ is the close-comment token\n"); 
	   printf("/* is the \"open-comment\" token\n"); */

Maybe you are not interested in using nested comments in such
"pathological" cases, but hey, NESTERs, don't you think sometimes
the ANSI-Standard Document for C is far too complicated to be
understood by any mortal (except Chris and Henry and a few others)?
Isn't it nice to have the simple rule: "A comment starts with
slash-asterix and ends with asterix-slash" POINT.

Would you really like to read: "A comment starts with slash-asterix.
Any further slash-asterix will increment a counter starting from
zero and any asterix-slash will decrement the counter. If the counter
eventually drops below zero, the comment is finished." Wouldn't you
then moan with regard to the last two lines in the above example,
that you could *not* simply comment out the code and ask: Why can't
C not look for double quotes within comments and *stop* delimiter
counting in this situation?

Again, the examples above may never be of any concern to you, but
some C Reference *has* to specify the language to the degree, that
the same source (hopefully) compiles to a program with the same
behaviour using different compilers. (Or at least it must point
out the "grey areas".) Again I ask you: Would you like this really
more than the current most simple rule?

To draw the bottom line I would propose: A compiler may well warn
about the sequence slash-asterix *within* a comment, we should use
#if-#endif for the purpose of 'commenting out' some part of the
source, but we should not want to have nested comments supported.
This should satisfy NESTERs and PURISTs.

Excuse me for this long posting, I've written it because
I hope it might keep some other traffic away from the net.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list