Page 1 of 1

Is this a no-no?

Posted: Tue Jan 23, 2007 12:47 pm
by daedalus__
Would it be a nono to use ternary operators like this:

Code: Select all

$this->testicles[(! is_null($order) ? $order : (count($this->Jtesticles) = 0 ? 0 : count($this->testicles)-1))] = 'hairy';
Would this method be preferred? Faster?

Code: Select all

if (is_null($order)
{
   $order = 0;
}
else
{
   $order = count($this->testicles)-1;
   if ($order < 0)
   {
      $order = 0;
   }
}
thankies!

Posted: Tue Jan 23, 2007 1:11 pm
by aaronhall
Your testicles are hard to read, but it's all the same to php

Posted: Tue Jan 23, 2007 1:57 pm
by Kieran Huggins
Just imagine the discomfort involved if you had to juggle your testicles around a little... You would be longing for the simplicity of convention... You could easily lose a testicle!

Re: Is this a no-no?

Posted: Tue Jan 23, 2007 2:19 pm
by Christopher
My threshold is readability and I certainly don't find your first example readable, whereas the second is completely clear. I tend to only use ternary operators for assignment in cases where they, in my opinion, make the code clearer. Mainly because an if() block (5-7 lines min) for a trivial assignment check (e.g. isset()) visually spoils readability for me. But I think where you draw the line and if you even use ternary operators is personal taste. I doubt there is any speed difference.

I would say in general rule if you are not sure -- do not use ternary operators.

Posted: Tue Jan 23, 2007 2:55 pm
by alex.barylski
I'm with arborint on this one...

trivial assignments only...where an IF just adds clutter...otherwise go with what sticks out immediately...

Posted: Tue Jan 23, 2007 3:42 pm
by John Cartwright
nested ternary statements are evil in my books.