Re^4: Why nested comments not allowed?

Derek R. Foster dfoster at jarthur.Claremont.EDU
Thu Feb 22 16:10:19 AEST 1990


In article <4015 at hub.UUCP> dougp at voodoo.ucsb.edu writes:
>-Message-Text-Follows-
>In article <1525 at wacsvax.OZ>, chris at wacsvax.OZ (chris mcdonald) writes...
>>dalenber at cbnewsc.ATT.COM (Russel Dalenberg) writes:
>>>In article <1523 at wacsvax.OZ>, chris at wacsvax.OZ (chris mcdonald) writes:
>>>	printf("A comment is ended with */");
>>Ho, hum, now your compiler, lexical analyser can't detect strings either?
>Comment this:
>
>printf("\"*/goto hell;/*\"");/* "\" is the escape charactor*/
>
>Build me a parser that handles this situation properly and I might
>begin to believe that nesting comments are possible. I personaly 
>prefer comment to end of line (like Microsofts //) for commenting
>out large blocks of code, it makes it easy to tell (on one of these
>damn 25 line displaies) just what has been commented out.

I can see why this example would be a problem - it's got a lot of potential
for errors if comments are placed anywhere near it. What I don't see is
any reason why it would be any less error-prone whether nested comments
are allowed or not. Just trying to comment this code out will get you
into trouble, no matter WHICH style of comments you use. (Your parser can't
assume that what's inside a comment is really C program code, so it can't
properly deal with an open quotation mark that occurs after a /* , and it
shouldn't assume that it's the start of a string. Therefore, */ will 
end a comment whether it's inside quotation marks or not)

Yes, this example is bad programming (Intentionally so, I assume), but
it's bad whether your compiler allows nested comments or not. (Turbo C allows
either). Having /* or */ inside a string is going to be a potential problem
no matter what. Why not just go

printf("\"*""/goto hell;/""*\"");

Then you are guaranteed of not having a problem. (Personally, I like nested
comments. They've saved me a lot of time and trouble. Sure, there are
examples that can be constructed that will not parse properly using them,
but I have yet to see one that didn't have severe faults just waiting to
happen under "normal" comments too.) If you write code that is fragile enough
to be broken by just moving comments around, the problem is with YOU, not
the parser. Putting /* and */ inside a string is an ugly (and potentially
dangerous) KLUDGE. Yes, it works sometimes, but (like all kludges) for the
wrong reasons, and for reasons that may no longer be valid if you (or
someone else) moves comments around in your program without knowing the
implied rules you set up when you used the kludge. 

The only real reasons there are to do this (put /* or */ in strings) are: 
  (a) making your source code strings visually contiguous (or at least AS
  contiguous as they can be in C, considering all the strange control codes)
  (b) saving a whole TWO characters in your source file.
Reasons NOT to do it:
  (a) You have to figure out whether /* and */ will be interpreted as
  a comment every time you use them. If the answer to this question is
  "Yes", you have to figure out some kludgey way to write your code so
  that the compiler won't interpret them as comments.
  (b) You can frequently no longer comment out certain sections of code
  (c) If you forget where you put /* */ , and later try to comment out
  sections of code, your compiler may (a) barf, (b) compile OK, but something
  you never intended (if, by some unlikely chance, you accidentally
  made your mistakes self-consistent). What happens if you accidentally
  leave out a quotation mark? or a (real) */ ?
  (d) Even if you personally never comment out code, there remains the
  chance that someone else will have to maintain your program. Unless you
  are willing to put warnings in your programs like "don't comment out
  function foobar - it contains comment characters", you're giving them
  a loaded gun.
  (e) There is a perfectly easy way to avoid problems (a) through (d) -
  just have the compiler concatenate strings (i. e. "a/*a" becomes "a*""/a".
  (f) just in case method (e) doesn't work for some specific instance, there are
  plenty of other ways in C to do this as well.

I just don't see that the benefits outweigh the disadvantages. Of course,
examples like this could no longer be used to argue for/against nested
comments...

End-of-line comments are nice too. I wouldn't trade them for normal ones,
however. (I'd prefer to have both)

Derek Foster



More information about the Comp.lang.c mailing list