Re: IF or SWITCH

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

 



On 4/5/06, Ray Hauge <ray.hauge@xxxxxxxxxxxxxxxxxxxxxxx> 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?
>
> Thanks,
> --
> Ray Hauge
> Programmer/Systems Administrator
> American Student Loan Services
> www.americanstudentloan.com
> 1.800.575.1099
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Without seeing more code it's hard to say which would be better for you to
use, but based on the snippet you've provided it looks like an if block is
the only thing that will work for you. The main perk to using switch over if
statements is speed (Google can back this up). The reason it's faster is
because it's simpler by design and is able to jump directly to the case that
evaluates to true, whereas an if statement needs to evaluate every if/elseif
condition until it finds one that evalutates to true.

In your example you're passing the $FFELP_Lenders variable through count()
multiple times. One way to speed things up would be to cache the results of
count($FFELP_Lenders) in a separate var so that the entire $FFELP_Lenders
array isn't being counted potentially 50+ times - unless of course you're
manipulating it later in the code.

You can also clean up this code a bit by removing other redundancies. In the
example below we're only evaluating $numFFELP one time. If it is not greater
than 1 then everything inside that block that depends on $numFFELPS being >
1 will be skipped - getting you one step closer to the speed of a switch
statement.


// Cache the number of elements in the
// $FFELPS_Lenders array
$numFFELP_Lenders = count($FFELP_Lenders);

if($numFFELP > 1) {

  // At this point we know $numFFELP is greater than one
  if ($numFFELP_Lenders > 1 && $numFFELP == $numTotal)
       $retVal = array(TRUE, 'A');
}else {

  // From here on out we know that $numFFELP
  // is greater than 0. If it's not this block of code
  // will be completely ignored.
  if($enumFFELP > 0 && $numFFELP_Lenders > 1 && $enumFFELP + $numFFELP ==
$numTotal){
       $retVal = array(TRUE, 'A');
  }elseif($numCONS > 0 && $numFFELP_Lenders > 1 && $numFFELP + $numCONS ==
$numTotal){
    // Rob says "Wheeeeeeeeeeeeeeeeeeee!"!!!!!11!!!11one!!!!1!
  }
}


It's been a long night for me as well so I hope this makes as much sense to
me in the morning as I think I does right now ;-)

[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