nesting coments

  • Thread starter Mantorok Redgormor
  • Start date
L

Leor Zolman

Breaks existing code. I've seen source where several lines, each
ending with a comment, are commented out by replacing the first two
characters (which were spaces) with "/*". For example:

/* x = foo(); /* get result of foo */

While I certainly don't *recommend* that, it is existing practice,
and changes to the standard usually seek to minimize adverse
effects on existing code.

OK, but I see nested comments as a personal time-saving device, and the
technique above as some kind of parallel time-saving device. Clearly the
two devices should not be placed in close proximity, and in any sane shop
they wouldn't be...or not for very long.
I don't see how inserting a

#if 0

and a

#endif

is any more trouble than inserting comment delimiters. It's a grand
total of 9 more characters (10 if you count the newlines, but
commenting out code without putting the delimiters on a separate
line is a barbarism not to be contemplated).

Well, and then there's finding the shift key, finding the # key (twice for
both), being in a hurry so having to backspace a few times while typing it
all out, .... ;-)
#if 0 is *better* than a comment for trying two different forms,
since you can put them into the two halves of a #if / #else and
switch between them by changing a single character (0 becomes 1).

Yes, there I'm now inclined to agree with you.
If by "preprocessor symbol" you mean a macro as the operand of
the "defined" operator (or of #ifdef), none is required. But you
*can* use one if you'd like to be able to select which form you're
using without editing the source (in typical implementations) - a
luxury not available if you use comments to remove code.


So long as it's a string of legal pp-tokens, you can do the same
with #if 0. No need for it to be legal C code.

Okay, some pros and cons. BTW, I don't use nested comments any more at all,
just because of the hassle of dealing with the compiler settings I'd have
to finagle across all the platforms I currently play musical compilers
with. But I'll probably end up adopting your #if 0 / #if 1/ #else
suggestion for switching alternate blocks of code.
Thanks,
-leor
 
K

Keith Thompson

I don't see how inserting a

#if 0

and a

#endif

is any more trouble than inserting comment delimiters. It's a grand
total of 9 more characters (10 if you count the newlines, but
commenting out code without putting the delimiters on a separate
line is a barbarism not to be contemplated).
[...]

Personally, I like comments that extend from a delimiter to the end of
a line, and cannot cross line boundaries (like Ada's "--", C99/C++'s
"//", and Perl's "#"). In any decent text editor (both emacs and vi
qualify), it's easy to comment out or uncomment a range of lines, and
the comment marker is clearly visible on each line. If you comment
out a section of code with "#if 0" ... "#endif", it's easy to miss the
preprocessor directives if you're looking at the middle of the
commented-out section. You can even (effectively) nest comments:

// some code that we're not currently using;
// // a comment about it
// and some more code that we're not currently using;
some code that we are using; // and a comment about it

I know, of course, that "//" comments aren't portable to C90
compilers, and that they can cause line-wrapping problems in code
posted to Usenet. But when I build my time machine, one of the
suggestions I'm going to make is to support "//" comments and *not*
"/* ... */" comments in C from the very beginning. (I'll also suggest
ignoring any whitespace between a '\' and the end of a line.)
 
L

Leor Zolman

Personally, I like comments that extend from a delimiter to the end of
a line, and cannot cross line boundaries (like Ada's "--", C99/C++'s
"//", and Perl's "#"). In any decent text editor (both emacs and vi
qualify), it's easy to comment out or uncomment a range of lines, and
the comment marker is clearly visible on each line.

I've done a lot of Perl hacking over the past three years, and I've sorely
missed C-style comments. I just now did a Google search and discovered
something that looks like:

=pod

blah blah

=cut

which I guess comes close to /* ... */. I guess I should have done that
Googling three years ago ;-)
-leor
 
M

Mabden

Keith Thompson said:
code;

I don't see how inserting a

#if 0

and a

#endif

is any more trouble than inserting comment delimiters. It's a grand
total of 9 more characters (10 if you count the newlines, but
commenting out code without putting the delimiters on a separate
line is a barbarism not to be contemplated).
[...]

That construct doesn't compile in C#, FWIW, so don't get too used to it.
As soon as MicroGates takes over the world, you will be assimilated.
Resistance is futile. For reals.
 
D

Dan Pop

In said:
posted to Usenet. But when I build my time machine, one of the
suggestions I'm going to make is to support "//" comments and *not*
"/* ... */" comments in C from the very beginning.

You'll have a strong case for that: IIRC, B supported // comments.
(I'll also suggest
ignoring any whitespace between a '\' and the end of a line.)

Even better, make \ outside of character and string literals a syntax
error, except for macro definitions.

Dan
 
D

Dan Pop

In said:
I've done a lot of Perl hacking over the past three years, and I've sorely
missed C-style comments. I just now did a Google search and discovered
something that looks like:

=pod

blah blah

=cut

which I guess comes close to /* ... */. I guess I should have done that
Googling three years ago ;-)

That's not a particularly bright idea, either. Some day, you may want to
use this Perl feature for its intended purpose (keeping Perl code and its
documentation together) and having PODs containing "garbage" will bite
you.

Do yourself a favour and read the perlpod man page...

Dan
 
L

Leor Zolman

That's not a particularly bright idea, either. Some day, you may want to
use this Perl feature for its intended purpose (keeping Perl code and its
documentation together) and having PODs containing "garbage" will bite
you.

I guess I'm really in danger of developing an inferiority complex if I keep
drawing the kinds of replies I've been getting from /you/ lately ;-)

Anything that would have helped me (during the countless hours of
debugging I've been doing with Perl scripts) to comment out large sections
of code easily would have indeed been a "particularly bright idea" (or at
least a useful one), and saved me much misery. I've never used this Perl
feature "for its intended purpose", nor do I suspect I ever will. As I
believe I must have mentioned several times now in various points in this
thread, I don't generally ship code with large sections of code commented
out (at least not on purpose). This hack would've been for my internal
debugging use only.
Do yourself a favour and read the perlpod man page...

I'll put it on my list, thanks.
-leor
 
D

Dan Pop

In said:
Anything that would have helped me (during the countless hours of
debugging I've been doing with Perl scripts) to comment out large sections
of code easily would have indeed been a "particularly bright idea" (or at
least a useful one), and saved me much misery.

And you couldn't figure out that Perl's goto was *exactly* what you
needed?

goto end_comment1;

perl code commented out

end_comment1:

By using a consistent naming convention for the label names (as suggested
above) you could immediately tell the difference between a "good" goto
(if you used any in your code) and the ones used for commenting out
large blocks of Perl code.

Dan
 
L

Leor Zolman

And you couldn't figure out that Perl's goto was *exactly* what you
needed?

Nope, never knew it had one. I was rather surprised to just see that Larry
Wall put in in there. (And I'm really *not* interested in hearing about why
you think I was wrong to be surprised.)
-leor
 
C

CBFalconer

Leor said:
On 14 Apr 2004 13:22:38 GMT, (e-mail address removed) (Dan Pop) wrote:
.... snip ...


I guess I'm really in danger of developing an inferiority complex
if I keep drawing the kinds of replies I've been getting from /you/
lately ;-)

Dan is noted for his diplomatic coddling and gentle chastisement.
 
K

Keith Thompson

Leor Zolman said:
In <[email protected]> Leor Zolman
That's not a particularly bright idea, either.
[...]

I guess I'm really in danger of developing an inferiority complex if I keep
drawing the kinds of replies I've been getting from /you/ lately ;-)

Don't worry about it. Apparently we're all inferior to Dan.

:cool:} :cool:} :cool:} :cool:} :cool:} :cool:} :cool:} :cool:} :cool:} :cool:} :cool:}
 
A

Arthur J. O'Dwyer

[Leor: using POD to "comment out" Perl code]
[Dan: don't do that]
[Leor: then what?]

And you couldn't figure out that Perl's goto was *exactly* what you
needed?

goto end_comment1;

perl code commented out

end_comment1:

By using a consistent naming convention for the label names (as suggested
above) you could immediately tell the difference between a "good" goto
(if you used any in your code) and the ones used for commenting out
large blocks of Perl code.

That's an even worse idea than the POD idea! First of all, if you
use a "consistent naming convention" for all those comment-block-
delimiting labels, you'll eventually slip up and type the same label
name twice. Then you'll have

goto foo;
blah;
foo:
bar;
goto foo;
baz;
foo:

about which 'perl' will not complain at all, according to the cursory
test program I just ran. So you'll get an infinite loop when you
thought you were just skipping down the page!
Secondly, 'goto' does NOT remove the offending code from the program!
It will still be not only parsed, but actually run: any 'my' variables
in the "commented-out" section will get declared, for example. So
you'll end up with errors at best, and silent bugs at worst.

The best way to "comment out" Perl code I can think of --- and I
don't pretend to be any good at Perl --- is to simply enclose the
offending code in a here-document, like this:

<<'COMMENT';
my $j = 42; # This variable will never be declared!
comment1: # This label will never be jumped to!
biguglypieceofcode; # This code will never be compiled!
COMMENT
;

Perhaps if you ask a Perl hacker, he'll tell you how to make that
construct more user-friendly with some kind of macro or something,
I dunno.

And just for the record, from the mouth of 'perlsyn' itself:
"One may also use pod directives to quickly comment out a section of
code."

(-:
-Arthur
 
D

Dan Pop

[Leor: using POD to "comment out" Perl code]
[Dan: don't do that]
[Leor: then what?]

And you couldn't figure out that Perl's goto was *exactly* what you
needed?

goto end_comment1;

perl code commented out

end_comment1:

By using a consistent naming convention for the label names (as suggested
above) you could immediately tell the difference between a "good" goto
(if you used any in your code) and the ones used for commenting out
large blocks of Perl code.

That's an even worse idea than the POD idea! First of all, if you
use a "consistent naming convention" for all those comment-block-
delimiting labels, you'll eventually slip up and type the same label
name twice. Then you'll have

goto foo;
blah;
foo:
bar;
goto foo;
baz;
foo:

about which 'perl' will not complain at all, according to the cursory
test program I just ran.

If you can't trust yourself to be able to generate unique comment block
delimiting labels, "grep foo: *" is all you need to check for clashes.
One doesn't need a PhD in CS to be able to figure this out...

I prefer the goto method because it works in all the languages I'm
currently using. Well chosen label names make its usage for this
particular purpose obvious. Of course, in C I use the idiomatic #if 0
(if the code is littered with #endif's, a unique comment used in both
directives helps matching them).
And just for the record, from the mouth of 'perlsyn' itself:
"One may also use pod directives to quickly comment out a section of
code."

That's the biggest problem with Perl: its documentation and tutorials
abound in bad advice. I started liking Perl only after realising that
I don't have to use the horrible style of the examples in the "camel
book" in my own code.

Dan
 
M

Michael Wojcik

That construct doesn't compile in C#, FWIW, so don't get too used to it.

It doesn't compile in Java, Fortran, COBOL, LISP, ML, or APL, either,
yet I'm quite used to it. But thanks for the suggestion - for what,
indeed, it was worth.
As soon as MicroGates takes over the world, you will be assimilated.

I suspect my component atoms will be scattered o'er the globe before
C# becomes the dominant member of the C family.

(And while I may not be an especial fan of Microsoft, I'm certainly
not worried that they'll be "assimilating" Micro Focus anytime soon.
If they wanted our market, they would have stayed in it, rather than
dropping it and making us a strategic partner.)
Resistance is futile. For reals.

But imaginaries still have a chance?
 
I

Irrwahn Grausewitz

[Leor: using POD to "comment out" Perl code]
[Dan: don't do that]
[Leor: then what?]
[Dan: use goto]
[Arthur: that's even worse]

(e-mail address removed) (Dan Pop) wrote:

[...]
I prefer the goto method because it works in all the languages I'm
currently using. Well chosen label names make its usage for this
particular purpose obvious.
[...]

I'm not a perl expert, but I just have to ask: how do you deal with
the other side effects Arthur mentioned:

AJO> Secondly, 'goto' does NOT remove the offending code from the
AJO> program! It will still be not only parsed, but actually run:
AJO> any 'my' variables in the "commented-out" section will get
AJO> declared, for example. So you'll end up with errors at best,
AJO> and silent bugs at worst.

Regards
 
A

Alan Balmer

It doesn't compile in Java, Fortran, COBOL, LISP, ML, or APL, either,
yet I'm quite used to it. But thanks for the suggestion - for what,
indeed, it was worth.


I suspect my component atoms will be scattered o'er the globe before
C# becomes the dominant member of the C family.

My father used to tell me sometimes "You'll be a man before your
mother is." It will be a very long time before C# is any kind of
member of the "C family", in spite of the bookstores which seem to
think it is. C# is much closer to Java than C. Yes, I'm aware that
some people consider Java a "better C", but that gets harder to
maintain with each new Java release.
 
D

Dan Pop

In said:
[Leor: using POD to "comment out" Perl code]
[Dan: don't do that]
[Leor: then what?]
[Dan: use goto]
[Arthur: that's even worse]

(e-mail address removed) (Dan Pop) wrote:

[...]
I prefer the goto method because it works in all the languages I'm
currently using. Well chosen label names make its usage for this
particular purpose obvious.
[...]

I'm not a perl expert, but I just have to ask: how do you deal with
the other side effects Arthur mentioned:

AJO> Secondly, 'goto' does NOT remove the offending code from the
AJO> program! It will still be not only parsed, but actually run:
AJO> any 'my' variables in the "commented-out" section will get
AJO> declared, for example. So you'll end up with errors at best,
AJO> and silent bugs at worst.

Show me a concrete example and I'll address it.

Dan
 
I

Irrwahn Grausewitz

In said:
[Leor: using POD to "comment out" Perl code]
[Dan: don't do that]
[Leor: then what?]
[Dan: use goto]
[Arthur: that's even worse]
[Dan: I prefer the goto]

I'm not a perl expert, but I just have to ask: how do you deal with
the other side effects Arthur mentioned:
Show me a concrete example and I'll address it.

Got me. Guess I'm not ready to expose my, *cough*, "skills"
in writing well-formed line noi^W^W perl code, not even in an
OT thread in c.l.c, sorry. :-/

Regards
 
T

Thomas Stegen

Alan said:
My father used to tell me sometimes "You'll be a man before your
mother is." It will be a very long time before C# is any kind of
member of the "C family", in spite of the bookstores which seem to
think it is. C# is much closer to Java than C. Yes, I'm aware that
some people consider Java a "better C", but that gets harder to
maintain with each new Java release.

But I saw a book once which covered C/C++/C# so obviously they must
be more or less the same language!
 
D

Dan Pop

In said:
Got me. Guess I'm not ready to expose my, *cough*, "skills"
in writing well-formed line noi^W^W perl code, not even in an
OT thread in c.l.c, sorry. :-/

For some strange reason, my Perl code is ressembling to my C code.

The best way for a C programmer to learn Perl is to get only the most
basic features (data types, operators, file I/O) and start writing code
using the C syntax. If the interpreter complains about syntax errors,
figure out the *minimal* changes required to make it happy.

Taking the K&R2 newbie program already discussed in another thread as an
example:

#!/usr/local/bin/perl -w

my ($fahr, $celsius);
my ($lower, $upper, $step);

$lower = 0;
$upper = 300;
$step = 20;

$fahr = $lower;
while ($fahr <= $upper) {
$celsius = 5 * ($fahr - 32) / 9;
printf("%d\t%d\n", $fahr, $celsius);
$fahr = $fahr + $step;
}

The only change I had to do in the "meat" of the program was adding the
dollar signs in front of each variable.

Dan
 

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
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top