in reply to variable set to 0 ? 0 : 1
Well. This is a pet peeve of mine.
Why do programmers try to do their job with as few brackets as possible? I would much rather write that as
return ( ( $status == 0 ) ? 0 : 1 );
To show that we are
- Testing status against zero, and
- Returning 0 if the test is successful, otherwise returning 1
Use more brackets and the explanation is much easier!
To put that into English, this returns 0 if the status value is 0; for all other values of status, it returns 1. So you could paraphrase this as "Did the routine return a non-zero status?"
--t. alex
but my friends call me T.
Re: Re: variable set to 0 ? 0 : 1
by sauoq (Abbot) on Sep 07, 2002 at 01:26 UTC
|
Because lots of brackets (parens to those of us on this side of the puddle) often make code harder to read even if they make it easier to explain. I would venture a guess that most veteran programmers aren't spending the majority of their time writing code that is meant to be read by novices.
They also make it more difficult to write. Your example, with two sets of parens, requires 8 more keystrokes if you count the shifts. That could add up to 12 inches or more of fingertip movement. When you write a lot of code that can be a very real concern.
I'm not at all suggesting that one should write the leanest code possible, and I want to make that clear. A good rule of thumb is that you should write your code so that someone with skills comparable to your own will be able to easily read it. Consistency in your style will help you achieve that more than anything else. As our style evolves, our code generally becomes less cluttered as a matter of course.
Of the variations below, all of which do the same thing, I prefer the last two.
return ( ( $status == 0 ) ? 0 : 1 ); # Too cluttered.
return ( $status == 0 ) ? 0 : 1; # Misleading.
return ( $status == 0 ? 0 : 1 ); # Clear
return $status == 0 ? 0 : 1; # Clean and clear.
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
|
return ( ( $status == 0 ) ? 0 : 1 ); # Readable, clear.
return $status == 0 ? 0 : 1; # Confusing. Precedence?
I *think* I know what the second line does, but I'm not 100% sure. Blame my 15 years of programming in C, but I have to use brackets when I write a return statement. If that means I'm writing C in Perl, so be it.
I also like to put brackets around a conditional, to highlight that there's a logic test going on. I know that operator precedence will take care of it without brackets, but why strain my brain.
Really, this is a tempest in a teapot. My Dad had a wonderful sentence that he claimed made sense, if only it were punctuated correctly: Smith, where Jones had had had had had had had had had had had the examiner's approval. Perfectly understandable: no punctuation is necessary, is it?
--t. alex
but my friends call me T.
| [reply] [d/l] [select] |
|
The point of formatting one's code is to make the meaning clear to the reader. (...) I *think* I know what the second line does, but I'm not 100% sure.
Hmm.. While I agree that some extra parens do no harm, I somewhat oppose the thought that using a languages (well documented) feature is "criminal". Someone's lack of specific experience or routine or different minting (no offence here) is not the "fault" of those more experienced/differently originated [uh - if this is wrong english, someone /mgs me please]. In the end, wondering about such constructs teaches us in the long run, right?
Then again, I actually agree with both of you... ;) While it's perfectly ok to add "markup" for your own/teammates convenience, to me it's ok, ethically, to exploit ones abilities and the abilities of the tool one is using (ahh.. well, unless you're a sniper with a precision gun).
Smith, where Jones had had had had had had had had had had had the examiner's approval
Well, I don't mean to be pernickety (but, hell, I am ;), but I'm not sure wether this example fits, since in that sentence punctuation is not optional. In English, for good reasons, punctuation -- especially to evade ambiguity -- is mandatory, while in Perl there are clear rules how to parse an (unambigous) term. However, I still wonder what the solution to that sentence is!? ;)
So long,
Flexx
| [reply] |
|
|
|
|
return
$status == 0
? 0
: 1;
Yes, it seems unnecessarily expansive for this simple case... but I probably wouldn't do it for this simple case.
I would, and do, do this for situations such as:
return
@things = $obj->get_current_list_of_things()
? grep /$wanted/, @things
: $obj->default_things;
# o.k., it's a contrived example. gimme a break.
Predicate and two lemmas, each on its own line.
| [reply] [d/l] [select] |
|
|