traveler has asked for the wisdom of the Perl Monks concerning the following question:
What is the collective wisdom here?expr && b(); # Short circuit operator b() if expr; # if clause if ( expr ){ # "traditional" if b(); }
--traveler
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Conditional style (if, &&) query
by George_Sherston (Vicar) on Oct 17, 2001 at 03:00 UTC | |
But if I was writing to somebody who knew the red button was a bad idea but didn't know exactly what was bad about it, I might write So my choice between B and C is You notice that here I used C, since we all know what the different options are, and what's at issue is under what conditions we use them. § George Sherston | [reply] [d/l] [select] |
by jackdied (Monk) on Oct 17, 2001 at 03:12 UTC | |
The '1' is a perl-ism. Perl will return the value of the last true value in the boolean. To prevent people from taking shortcuts and expecting the sub to return false or the value of $red_button_pressed or $massive_failure we return one. Sooner or later it will no longer return what they think it does, and their code will melt (see also bitrot). -jackdied | [reply] [d/l] |
by blakem (Monsignor) on Oct 17, 2001 at 03:21 UTC | |
is identical to is identical to So I don't follow your last paragraph...
Update which will "coerce" all TRUE responses to be 1 instead of the value of $b? -Blake | [reply] [d/l] [select] |
by Elliott (Pilgrim) on Oct 17, 2001 at 12:53 UTC | |
It may be prejudice that makes me prefer this structure. But I do think there is good reason too. George_Sherston's comment about the relative importance of the things_to_do is a good one and I take this into account by making free use of unless() {}, but there's another issue here, for which I will put on my tester's hat. It is important that we see clearly that it is a condition. From the perspective of a test designer, the presence of a condition is more important that what it does. And since I think better code gets written if everyone who comes into contact with it thinks about it from a tester's perspective, that makes "C" style a clear winner for me. BTW SInce I was using the "C" style a long time before C was invented, I find it hard to accept the phrase "traditional C style"!! | [reply] [d/l] |
Re: Conditional style (if, &&) query
by dragonchild (Archbishop) on Oct 17, 2001 at 02:47 UTC | |
I personally use B, simply because it's succinct and easy to read, but doesn't bring in lesser-known quantities of the application. If you're going to be working with less-skilled people, use C every time. The idea is to work with them, not show them how much you know (though demonstrating nicely new techniques is always reccomended ... especially for those times when you end up being charged for whitespace. *grins*) ------ Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. | [reply] |
Re: Conditional style (if, &&) query
by hopes (Friar) on Oct 17, 2001 at 02:58 UTC | |
Option B is "more readable" for me, but there are situations in which I use option C. I use option C if expr is long or complicated. I use option C also if there is one possibility to need one else now or later. I think I am a little fussy, cause I don't like I prefer because I find that really more readable. Err, I think :-) Hopes | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by demerphq (Chancellor) on Oct 17, 2001 at 03:14 UTC | |
The second I like especially if the 'function' is a flow control keyword like next,last or redo or return. Also where there is no else and the if is expected to most often succeed, or on the contrary where I want to stylistically emphasize something. Like The third I would do if I thought there was the slightest possibility I would need to have an else or multiple statements. Having said that, generally if there is no need for an else block my reasoning for using the second form usually already applies. Oh yeah, if IIRC this type of stuff is covered in perlstyle quite nicely I thought. HTH
Yves | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by Hutta (Scribe) on Oct 17, 2001 at 06:01 UTC | |
Works for me because I see the "$debug" and mentally disregard that line from the regular functionality of the code. ...doesn't stand out as much as debugging because I tend to scan code down the left side. ...that way just eats up way too much room when there's a lot of debugging output. And of course these are all for smaller scripts where a more complete debugging output solution isn't needed. | [reply] [d/l] [select] |
by Hofmator (Curate) on Oct 17, 2001 at 14:47 UTC | |
Good point you make here but let me mention one thing in the context of debugging in this way. If you make $debug a constant then you don't get any runtime speed penalty for these kind of tests. They are optimised away at compile time:
Of course, you no longer have the option of specifiying your debug level on the command line. Sean M. Burke has a nice article about constants in perl (including this usage) here at The Perl Journal. We also had some discussion about that topic here at PM, e.g. in inlined DEBUG constant versus $DEBUG. -- Hofmator | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by Toodles (Monk) on Oct 17, 2001 at 07:28 UTC | |
Option B is clean, but doesn't translate to English as clean as I would prefer. In general, I prefer the 'if' and 'unless' clauses after the statement only under rare circumstances. Because the statement is in front of the condition, the statement appears more important to the human mind. So, only use it when it is very likely that the code block WILL be run. For example: This same idea goes for unless and or: Remember, 'unless' is much more readable to most new coders than 'if(!$a)' Never put the conditional after the code block if the code block is more than one statement.... Although it is a subset of answer C, my personal prefered method of seeing this is: Since the actual expression and the block to be executed are small, put them on one line; Easy to read, translates cleanly to mental English, and easy to add 'else' and other statements in the future. It also doesn't hog 3 lines with mostly whitespace. Again, keep in mind that this is all a matter of personal preference, and no one is right and wrong. Make your own code and goes with what makes the most sense to use. From my understanding of corporate coding jobs, their guidelines will take all that guess work out for you. :) Toodles | [reply] [d/l] [select] |
by blakem (Monsignor) on Oct 17, 2001 at 08:47 UTC | |
-Blake | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by bjohnso (Sexton) on Oct 17, 2001 at 02:46 UTC | |
I personally find it a LOT easier reading the traditional C style if statements. They just make code look SO much cleaner and easier to follow. My other gripe is comments. The more comments the better. I personally hate coming back to my code after 6 months and having to remember what assumptions I made when I wrote it. So I guess my vote is for:
| [reply] [d/l] |
by tilly (Archbishop) on Oct 17, 2001 at 04:17 UTC | |
I strongly think not, and that is a gripe of mine. Don't get me wrong. I use comments, and on the whole I have quite a few of them. But to me comments are something which has to be done right or else it is worse than useless, and I would prefer to see completely uncommented code to overly verbosely commented code. For a thread that outlines my personal commenting style and the reasons for it at some length, I would suggest reading Re (tilly) 2 (disagree): Another commenting question,. (Note that behind the scenes we traded a series of /msgs that were more friendly than the discussion there.) After reading that, if you think that I really need more comments than I use, or that my points about how comments can be a problem are off-target, please give me feedback. | [reply] |
by Biker (Priest) on Oct 17, 2001 at 15:36 UTC | |
I beg to disagree with the statement
"The more comments the better.".
| [reply] |
Re: Conditional style (if, &&) query
by tommyw (Hermit) on Oct 17, 2001 at 02:50 UTC | |
It's your choice: that's the beauty of Perl. That said, I don't think I've ever seen the first version in use (although it's common with or aka ||). Of the other two, it depends on the complexity of the expression, the function call, and which you think is "more important", and you'd therefore like to read first in your code. | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by chipmunk (Parson) on Oct 17, 2001 at 05:57 UTC | |
expr and b(); is good when the expr is more significant than the call to b(), or when you're emulating a switch statement. b() if expr; is good when the call to b() is more significant than the expr, such as b() if $DEBUG;. if ( expr ) { b(); } is good because it has the clearest structure, especially for people familiar with other languages. | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by DamnDirtyApe (Curate) on Oct 17, 2001 at 05:55 UTC | |
Not the most complex logic in the world, but IMHO you've already lost a lot of readability. As for the other two, I like the b() if expr; when there's only one expression to do, and the traditional if block any other time. Also, I personally find Stroustoup-style indenting/bracketing the most readable, ie. I'm aware that this bracketing isn't the convention in Perl (if anything is a convention; TMTOWTDI), but it's my personal preference. What do others think? | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by petdance (Parson) on Oct 17, 2001 at 08:04 UTC | |
I usually try to sound as English-like as possible, and keep in mind what I'm trying to emphasize: the test or the action? There's no One Right Way, even within the same program or even the same block of code. Go with what makes the most sense for the specific piece of code. Also, consider the surrounding code. Something as simple as conditionally printing a phone number can depend greatly on why it's doing it. The "standard" way might be: Here's some code where that probably is best: We check the phone number, and make sure it's a valid format, and then when we're done, we might print the phone number. Whether we print or not is incidental to the rest of the block. But consider this instance where the context is entirely different.
Here, it's more appropriate to show that we're printing a bunch of information, and the phone number is optional. Writing it the "old way", with the phone number block taking three lines, would break up the visual flow, like so: Blech. I think rule two of Perl, after TMTOWTDI, should be "It all depends."
xoxo, | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by rrwo (Friar) on Oct 17, 2001 at 14:19 UTC | |
I would say what is most readable has to do with the "flow of thought" as to what you're doing. Perl is an "expressive" language in that you can say the same thing many ways. My rule of thumb: It's readable if a person with a decent knowledge of Perl who is going bleary eyed debugging the program for the last 12 hours can still make sense of the code and not mis-read it, and if the code is clear enough that it requires minimal comments. I usually avoid short-circuit expressions because they are often easily misread. | [reply] |
Re: Conditional style (if, &&) query
by Maclir (Curate) on Oct 17, 2001 at 19:22 UTC | |
I would tend to go with option c (the traditional if structure) for the following reasons: | [reply] [d/l] |
Re: Conditional style (if, &&) query
by willdooUK (Beadle) on Oct 17, 2001 at 14:32 UTC | |
Then when someone else is looking through your code they can get used to your style no matter how alien it is to them. It's like accents - you can very quickly adjust to someones accent when they speak, but if their accent were to change every 10 seconds, you'd get lost. Wow. I just read that back and I think I need more coffee... :) willdooUK -------------- "Home is a castle you built in my mind; I'm home anywhere, anytime." Donny Hathaway | [reply] |
Re: Conditional style (if, &&) query
by Biker (Priest) on Oct 17, 2001 at 15:40 UTC | |
I frequently tend to use 'A', because I like it.
| [reply] |
(jeffa) Re: Conditional style (if, &&) query
by jeffa (Bishop) on Oct 31, 2001 at 04:09 UTC | |
Consider this nasty block: To this, much nicer block: Who even needs if? :D (um, sarcasm) jeffa | [reply] [d/l] [select] |
Re: Conditional style (if, &&) query
by barndoor (Pilgrim) on Oct 17, 2001 at 19:15 UTC | |
Some conditions read better using B and some using C. I've never bothered using A. | [reply] |