Re: Re: SHOULD I NOT USE "ELSE" IN IF STATEMENTS....?
| From: | Dee Ayy | Date: | Mon, 08 Jun 2009 16:40:50 +0000 |
| Subject: | Re: Re: SHOULD I NOT USE "ELSE" IN IF STATEMENTS....? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-293746@lists.php.net to get a copy of this message | ||
It's for better code.
Personally, I'm trying to get away from multiple return/exit paths,
and practice more OOC/Eiffel-like coding (I haven't sprung for the
book Object Oriented Construction yet
http://archive.eiffel.com/doc/oosc/page.html
) and I'm not quite
taking advantage of Design By Contract in my Eiffel coding yet though
either.
At a minimum, I would change this:
function doThisAndThat($bTrueOrFalse)
{
if ($bTrueOrFalse)
{
return 'It is true';
}
/* If the above is true we can be sure it's NOT true here. */
return 'It is false';
}
to this:
function doThisAndThat($bTrueOrFalse)
{
$return_value = 'It is false'; // Set your default state here.
if ($bTrueOrFalse)
{
$return_value = 'It is true'; // Update state.
}
return $return_value; // Only 1 exit path.
}
I've also noticed that, for the most part, PHP itself codes
functions_or_variables_like_this, while coming from Java, my PHP code
looksLikeThis.
Slightly more readable for say do_www_design versus doWWWDesign versus
do_wwwd_esign for those times when you run into such names.