Binary Expressions

2008-10-8

Using short if statement in programming

Filed under: — Adam @ 2:50 pm

In many programing languages it is possible to shorten if statements using what’s called the ternary operator. It is sometimes referred as the “one line if statement” or the “short if statement”. This can help at times to produce cleaner code, however use this operator wisely as it is not always best to be used for more complicated statements.

PHP Example of an if statement


1
2
3
4
5
6
7
8
if($nFoo > 0)
{
   echo "I'm at the work.";
}
else
{
   echo "I'm at home.";
}

PHP Example using the ternary operator


1
echo $nFoo > 0 ? "I'm at the work." : "I'm at home.";

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Source: Wikipedia on Ternary Operator
Source: PHP Manual: Comparison Operators

12 Responses to “Using short if statement in programming”

  1. Joe Izenman Says:

    In PHP you can also pull those echo statements out and have the ternary statement just return a string to a single echo, for incrementally cleaner code:

    echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;

  2. Bill Karwin Says:

    One drawback to using the ternary operator is that you now have a logic branch in a single “line” of code. This makes it hard to measure code coverage from tests, for example. It also makes diffs less clear when you commit code changes to source control.

    I never use the ternary operator when each predicate is a full statement of code. I use the ternary operator only when I need each predicate to return a value because the whole expression returns a value.

    As soon as you need either predicate to be two statements, you must rewrite the expression into a full if/then/else construct anyway.

    It should not be your goal to write code that has fewer lines. It should be your goal to write code that is easier to test, easier to change, easier to document, easier to maintain.

  3. JulesBravo Says:

    echo ( $nFoo > 0 ? “I’m at the work.” : “I’m at home.” );

    I think that’s better syntax.

  4. Johannes Says:

    Your second example won’t work, echo can’t be used inside an expression, youd have to use print (that’s the only difference between the two btw., print always returns true and vcan be used in expressions, echo can’t) or you have to write the echo outside the ternary, which i consider nicer.

    echo $condition ? “yes” : “no”;

    But don’t abuse ternaries, I saw people widely nesting them - unreadable that is :-)

  5. K Jordan Says:

    Especially since PHP does it backwards with regards to that if you have nested ternary operators.

    You could also eliminate it down to one echo and just have it evaluate which string to echo out.

  6. Rob Wultsch Says:

    I have seen some horrific uses of the ternary operator. I would rather it not be used than abused.

    Something I wrote on the topic:
    http://wultsch.com/blog/2008/08/17/why-php-sucks/

  7. Jeremy Peterson Says:

    I used to hate the ternary operator, because it puts two possible values for a variable on one line and I’d like to keep my style for if/else statements consistent.

    It is much easier to put these lines of code on one line than taking up six lines of code. Not only is it a matter of space, but for simple variable definitions it makes it easy to have a default value.

    Ex:
    $page_num = ($_REQUEST[’page’]) ? $_REQUEST[’page’] : 1;// Default 1st page.

    Jeremy

  8. Thomas CORBIERE Says:

    This example doesn’t work as echo is not an expression and the ternary operator expects his arguments to be expressions. The correct example should be :

    echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.” ;

  9. LGB Says:

    $nFoo > 0 ? echo “I’m at the work.” : echo “I’m at home.”;

    This is the overcomplicated version, still. You can use this:

    echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;

    Or even:

    echo “I’m at “.($nFoo > 0 ? “the work”:”home”);

  10. hartmut Says:

    A better example would have been

    echo ($nFoo > 0) ? “I’m at the work.” : “I’m at home.”;

    as one of the main differences between an if block and a ternary (besides the amount of keystrokes needed) is that the ternary is an expression and so has a result value.

    IMHO it is also good style to put ternary parts on different lines unless they are *really* short like “$leap_year ? 29 : 28″:

    echo ($nFoo > 0)
    ? “I’m at the work.”
    : “I’m at home.”;

  11. mysql Says:

    You also move “echo” function out of statment:

    echo $nFoo > 0 ? “I’m at the work.” : “I’m at home.”;

    I personaly add braces when using this type of statment to make it more readable:

    echo (($nFoo > 0) ? “I’m at the work.” : “I’m at home.”);

  12. Adam Says:

    Thanks to everyone for the great feedback! I have made changes to the ternary operator example to remove the unnecessary echo statements.

Leave a Reply

Take back your mailbox - CAUCE.org

Powered By Wordpress PHP: Hypertext Preprocessor MySQL Powered Download Juice, the cross-platform podcast receiver
Proud To Be Canadian Get Firefox Valid XHTML Valid CSS
<NO>OOXML Logo


24 queries. 0.585 seconds.
Copyright © 2004 - 2005 by Adam Douglas