Re: IF or SWITCH

From: Date: Thu, 06 Apr 2006 15:50:02 +0000
Subject: Re: IF or SWITCH
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-233378@lists.php.net to get a copy of this message
At 7:48 PM -0700 4/5/06, Ray Hauge wrote:
Hello World! wait, not coding... (sorry, long night) Okay, I finally finished hashing out all the logic for a very complex set of rules to determine what "type" an application should be set to. I won't bore you with the details of it, but the question is... I have 57 if/elseif/else statements because of all the different criteria. Is it considered better programming practice to use if/elseif/else statements over a switch(true) case (true && false || true || false) syntax? Basically, I'm not too happy with the readability of the code, but I'm afraid that at this point there's not much I can do... code snippet: if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && $numFFELP + $numCONS == $numTotal){ etc. Any suggestions?
Switch. Regardless of speed, I find that switch is much easier to write and debug than if/elseif -- which, regardless of my shortcomings, I never use. I can't stand using elseif's and have never ran into a problem that required their use -- can anyone show me one where a switch would not do just as well, if not better? As for the above, what's wrong with the following? $who_cares = 1; switch ($who_cares) { case $numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; case $numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; ... } OR switch ($numTotal) { case $numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP: $retVal = array(TRUE, 'A'); break; case $numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP: $retVal = array(TRUE, 'A'); break; ... } <running for cover> tedd </running for cover> -- -------------------------------------------------------------------------------- http://sperling.com

« previous php.general (#233378) next »