Early return (was: Inspiration for a Tombstone.)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2008/6/29 Colin Guthrie <gmane@xxxxxxxxxxxxxx>:>> In these instances you could rely on != behaviour instead of ==>> behaviour, like this:>>>> <?php>> if ($challenge_password_hash != $stored_password_hash) {>>   echo 'Stay out! This club is for members only!';>> } else {>>   echo 'Welcome to the club!';>> }>> ?>>>>> or, better yet:>>>> <?php>> if ($challenge_password_hash != $stored_password_hash) {>>   echo 'Stay out! This club is for members only!';>>   exit;>> }>> echo 'Welcome to the club!';>> // Lots of code here that just saved itself another indent in my IDE>> ?>>> Indeed. The technique obviously only works for constants, and it wont help> with variable -> variable comparisons (unless you do something really stupid>  like put the first expression in quotes!).>> Using != when possible is a good idea but I guess you have to draw the line> as to moving your preferred "flow" of logic around to fit in with a> technique for reducing the possibility of logical errors.>> Even your example above hints at another "political" minefield - Early> Return or Multiple Return Points.... (s/Return/Exit/ in this case) Let's not> even go there!!!>
Why not? I do this often, but I am not a professional programmer. Ifind this to be very useful.
Dotan Cohen
http://what-is-what.comhttp://gibberish.co.ilא-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת;
A: Because it messes up the order in which people normally read text.Q: Why is top-posting such a bad thing?

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux