Re: Wrapping long if statements

From: Date: Tue, 16 Nov 2004 18:22:49 +0000
Subject: Re: Wrapping long if statements
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34413@lists.php.net to get a copy of this message
Jesper Veggerby Hansen wrote:
Hi Guys, Been looking a bit on CS and was wondering how you would wrap a very long if-statement (line-length) while still maintaining readability. Fx. if (($a == 0) and ($b == 0) and ($c == 0) and ($d == 0) and ($e == 0)) {
    // do something
} I think if you wrap it to something like this it makes it a lot less readable since the indention of the conditions are the same as the statements to within the if-statement, i.e.: if (($a == 0) and ($b == 0) and ($c == 0) and
    ($d == 0) and ($e == 0)) {
    // do something
}
I like doing: if (($a == 0) && ($b == 0)
    && ($c == 0
        || $d != 0)
) {
    // do something
} That is, putting the operand at "start" of line and indenting for more complex sub-expression, so grouping is visually easy to spot.
Now while I'm at it, how would you wrap a trinary-operator statement, i.e. $i = ($a ? $b : $c); (Assuming it's long), would you go for: $i = (
    $a
    ? $b
    : $c
);
$i = ($a)
     ? $b
     : $c;
With indentation for $b and $c expression if needed... By habit, I always put the condition in parens (as for a "if" statement.) Since it's not in CS, it's just a matter of taste. What's important is consitency in your code. Choose a reasonable clear solution and stick to it. -Philippe

« previous php.pear.dev (#34413) next »