Re: Which is your favourite cmt-deserving Perl feature?
by tlm (Prior) on Jul 21, 2005 at 22:21 UTC
|
I figure that if an idiom is so mystifying that it needs a comment, I'd rather not use it. For the most part I use idioms to save myself typing; it defeats this purpose to have to type out comments to explain them.
| [reply] |
Re: Which is your favourite cmt-deserving Perl feature?
by siracusa (Friar) on Jul 21, 2005 at 20:38 UTC
|
You said it in your original post, but here's the canonical example:
while(<>)
{
next if 1..1; # skip first line
...
}
| [reply] [d/l] |
|
|
Eeew. Classical example of how not to program. About the first thing I learned when learning how to program efficiently is to take anything out of the loop that doesn't need to be in the loop. If you want to skip the first line, skip it outside the loop, don't test for every line whether it'll be the first line or not.
<>; # Skip first line.
while (<>) {
...
}
| [reply] [d/l] |
|
|
Except that this is not an equivalent loop if there is only one line to be read. If you read that one line, @ARGV is now empty, and when you enter the loop, the ARGV loop will now be reading STDIN! Oops! Doh!
| [reply] |
|
|
|
|
|
|
Ouch! Not only this is the .. operator in scalar context, which is somewhat esoteric as of itself, but it is a special case of it which is even more esoteric, and used with both limits equal to 1 which further adds to its esoteric nature! Thus: nice example indeed! And nice golfing technique - I'll keep it in mind. But I guess that I would rather write
next if $. == 1;
in most other situations... | [reply] [d/l] [select] |
Re: Which is your favourite cmt-deserving Perl feature?
by diotalevi (Canon) on Jul 21, 2005 at 17:50 UTC
|
Comment on what "CMT" is. | [reply] |
|
|
Heh. Hint: you just said it.
| [reply] |
|
|
Hmmm "CMT" ne "cmt".
Leaving aside jokes, I must say that I can't stand too people who spells everything like a dude, a' la "can u plz tell me if u can do this or that b4..." But I thought that "cmt", especially used only once, would have been an acceptable and commonly used abbreviation.
| [reply] [d/l] |
|
|
| [reply] |
Re: Which is your favourite cmt-deserving Perl feature?
by jdhedden (Deacon) on Jul 21, 2005 at 19:07 UTC
|
I would comment the following idiom:
# If not already done, initialize $something using whatever()
$something ||= whatever();
Remember: There's always one more bug.
| [reply] [d/l] |
|
|
| [reply] |
|
|
$u ||= '';
# or
$v ||= 0;
(Most often this is not necessary either, and -as usual- there are other ways to do it...) | [reply] [d/l] [select] |
|
|
If used (I do it often), that's something that would be spread widely in the application.
So if it's used, it would be considered a known idiom and shouldn't be commented everywhere in the code.
If in a team environment, this would be considered team lore and any newbie could easily just ask someone the first time it's encountered.
/J
| [reply] |
|
|
# If not already done, initialize $something using whatever()
$something ||= whatever();
I have to admit that this is the sort of comment I hate :-) It doesn't tell me anything that the code doesn't. | [reply] [d/l] |
Re: Which is your favourite cmt-deserving Perl feature?
by Anonymous Monk on Jul 22, 2005 at 15:58 UTC
|
I'd comment what I meant whenever I'd use the word cmt. | [reply] |