Re: Wrapping long if statements

From: Date: Mon, 15 Nov 2004 10:22:29 +0000
Subject: Re: Wrapping long if statements
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34393@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
} Now while I'm at it, how would you wrap a trinary-operator statement, i.e. $i = ($a ? $b : $c);
Generally we dont recommend the shorthand syntax. It tends to be less readable .. but imho there is a fine line where it is actually more readable since you can immediatly see that the if is only trying to figure out the value to assign to a specific variable. Beyond that we also use && and || instead of AND and OR although I dont know if either of those are in the pear CS (look it up in the manual). As for wrapping we dont have a standard afaik. I have encountered different variations. My personal favorite is the following: if($foo
    && $bar
) {
    /* code */
} the idea being that I put the most important part of the if at the start and all following blocks can be easily commented out if needed during development like if($foo
#    && $bar
) {
    /* code */
} The other huge advantage is that you have a fixed position where you can expect && and || etc. Even if you are good at breaking stuff after 80 chars it helps readability imho. Finally the ) { on the last line makes it easy to spot multiline conditional blocks and again enables me to also easily comment out the final condition. regards, Lukas

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