Page 3 of 4
Posted: Mon Jan 08, 2007 4:42 pm
by Luke
onion2k wrote:I nest ternary statements.
I do too
but I try to do it like this:
Code: Select all
$var =
($expression)
? true :
($expression)
? true :
false
so it's at least a little less confusing
Posted: Mon Jan 08, 2007 4:46 pm
by mikesmith76
I nest ternary statements.
What's wrong with nesting them, apart from making the code a little bit more difficult to read?
I find myself using them all the time with things like
Code: Select all
$var = (isset($_GET['var']) && ctype_digit($_GET['var']))?$_GET['var']:false;
Posted: Mon Jan 08, 2007 4:53 pm
by jayshields
Personally, I find that hard to read.
What's wrong with:
Code: Select all
if(!isset($_GET['var']) || !is_numeric($_GET['var'])) $_GET['var'] = false;
I used is_numeric instead of ctype_digit because I've never heard of the latter and cannot see the difference.
EDIT | Ok, ctype_digit means every character in the string must actually be a digit, whereas is_numeric has support for hexadecimal numbers and what not. Still would go with is_numeric because it's alot clearer what the function does.
Posted: Mon Jan 08, 2007 5:00 pm
by mikesmith76
Don't think there's anything wrong with either approach, just down to personal preference. Got used to using the ternary style but I suppose it doesn't make any difference either way.
Posted: Mon Jan 08, 2007 5:40 pm
by RobertGonzalez
Other than the fact there are a few more lines involved, why would you not want to do this:
Code: Select all
<?php
if ( isset($_GET['var']) && ctype_digit($_GET['var']) ) {
$var = $_GET['var'];
} else {
$var = false;
}
?>
instead of this:
Code: Select all
<?php
$var = (isset($_GET['var']) && ctype_digit($_GET['var']))?$_GET['var']:false;
?>
Posted: Mon Jan 08, 2007 5:45 pm
by jayshields
Everah wrote:Other than the fact there are a few more lines involved
That's the only reason. Personally, I think my solution is the most readable and line-friendly

Posted: Mon Jan 08, 2007 6:06 pm
by feyd
Line-friendly?
More readable for who?
I try to write my code so newbs can read it and follow the logic.
Posted: Mon Jan 08, 2007 6:17 pm
by jayshields
feyd wrote:Line-friendly?
More readable for who?
I try to write my code so newbs can read it and follow the logic.
Couldn't think of a good phrase in place of line-friendly!

Y'know what I mean.
More readable for newbs, but the more lines you have, the more readable it is. So you have to strike a happy medium, which I think my code does.
In my opinion.
Posted: Mon Jan 08, 2007 6:37 pm
by Ollie Saunders
Couldn't think of a good phrase in place of line-friendly! Smile Y'know what I mean.
'fraid I don't. Who cares about lines of code anyway?
You don't actually need ternary for anything you know, its just a shorthand. I find myself using it on occasion most of the time I can force myself to use an if..else and it looks better for it when I can't force myself to use if..else is usually because I'm feeling lazy -- which is a
really 
great motivator for good code.
Posted: Mon Jan 08, 2007 8:28 pm
by RobertGonzalez
feyd wrote:I try to write my code so newbs can read it and follow the logic.
Amen to that brother!
Readable code, for me, is understandable code. Same as commenting, in my opinion. What the code does not explain clearly the comments do, but I would hope that no one looking at my code would ever look at it and scratch their head asking what that means.
Posted: Tue Jan 09, 2007 12:32 am
by matthijs
The only place I use ternary is in forms, like:
Code: Select all
<input type="text" name="name" value="<?php echo isset($_POST["name"]) ? htmlentities($_POST["name"], ENT_QUOTES, 'UTF-8') : '0'; ?>" />
That's a tiny bit more readable then having multiple lines of an if else loop mixed in. However, there are probably better ways to do this

Posted: Tue Jan 09, 2007 3:14 am
by mikesmith76
I use them (and will continue to do so) as I don't see what is difficult to understand about them. Have never come across a need to next them tho, but i imagine that could become messy
Posted: Tue Jan 09, 2007 3:42 am
by Maugrim_The_Reaper
Using ternary style in PHP, is like using l33t speek in ur lang.
Shorter is harder to read by other people. I agree with Everah and Feyd - and I also have banned ternary and on occassion even the short single line if() condition in a coding standard. Sure longer versions of conditions take longer to type, but they often make things more obvious. In my view, if I have to slow down to decipher a piece of code I'm more likely to rewrite it on the spot to a) figure it out, and b) not have someone else down the line spend the extra seconds figuring it out. We developers are supposed to be lazy people, but sometimes that laziness can bite you in the future.
Posted: Tue Jan 09, 2007 5:13 am
by wei
i think ternary are readable when used with simple assignements, e.g.
Code: Select all
$isMale = ... some complicated boolean expression to define a male...;
$name = $isMale ? 'Bob' : 'Jane';
Posted: Tue Jan 09, 2007 11:19 am
by Luke
wei wrote:i think ternary are readable when used with simple assignements, e.g.
Code: Select all
$isMale = ... some complicated boolean expression to define a male...;
$name = $isMale ? 'Bob' : 'Jane';
I agree