Re^2: Why nested comments not allowed?

Todd M. Lewis utoddl at uncecs.edu
Tue Feb 20 01:36:19 AEST 1990


In article <7330017 at hpcllak.HP.COM> shoe at hpcllak.HP.COM (Jim "Shoe" Schumacher) writes:
>>ly at prism.TMC.COM /  8:21 am  Feb 14, 1990 /
>>	I'm just curious to know why nested comments are not allowed in many 
>>	languages.
>You can have some subtle errors with nested comments. 

You can have subtle errors with "flat" comments.
    ready_level = FULL_ALLERT;  /* Can't disarm except in this state * /
    status = disarm_missle();   /* who would have thought it could happen! */
Look closely at the comment.  Yes, there's only one comment.  If the compiler
had supported nested comments, this wouldn't have compiled, run, and "bombed".

>{ 
>	this was a real **{??/ to write and .....   <- start of nesting
>}
><- rest of the file is ignored while we search for the matching brace
>   and the compiler ends up giving you some really strange error message
>   about unexpected eof. You go through your code and can't figure out
>   what it's complaining about. 

You have a broken tool.  Any compiler worth its salt should tell you
where an unclosed comment begins.  Everybody knows where it ends.
Claiming nested comments are no good because your compiler doesn't know
how to handle them doesn't hold water.  That would be as bad as not
telling you what #ifdef wasn't #endif'd (see comment below).
Or for that matter, it's as bad as not dealing with improperly nested { }.

>When nesting is allowed you can no longer place any characters you want
>in your comments since your text is being scrutinized by the compiler. 

You can't do that now--at least you can't put "*/" in.

>The need to nest comments usually arises when you want to comment out
>a section of code that contains comments. A better way to do this is 
>to use a compiler directive like #ifdef.
>#ifdef 0
> code with comments....
>#endif

Because we've been doing it that way for eons doesn't mean it's a
"better way."  Of course, without true nested comments it's the
only way, but that, too, is beside the point. #ifdef/#endif is better
for conditionally compiling code fragments.  Nested comments are the
better way to "comment out a section of code that contains comments."
Your own words--it should be obvious.  I am continually amazed that
the C community, which claims a plethora of operators is good and that
professionals should be able to handle them, which can nest {}, [], (),
and #ifdef/#endif in myriad ways, which declares pointers safe if used by
responsible programmers, could be intimidated by the concept of something
as benign, useful, and elegent as nested comments.  Sounds like a lot of
NIH to me.
  Sure, nesting of comments can be accomplished with #ifdef/#endif.  So what?
while and for can be accomplished with goto.  But it's the wrong tool.

>jim shoe (shoe at hpda)

Please don't read this as a personal attack on Jim.  He just hit a nerve.

-- Todd M. Lewis



More information about the Comp.lang.c mailing list