Is this a no-no?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Is this a no-no?

Post 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!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Your testicles are hard to read, but it's all the same to php
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is this a no-no?

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nested ternary statements are evil in my books.
Post Reply