tags:

views:

44

answers:

4

I need to solve a problem like below. I'm not sure, can you suggest better method?

switch(true){
    case $price>100:
        tooHigh(); 
    break;
    case (($price<=100) && ($price>70)):
        negotiate(); 
    break;
    case (($price<=70) && ($price>20)):
        accept(); 
    break;
    case ($price<=20):
        thankAndEscape(); 
    break;
}
A: 

Use do-while.

do {

if($price > 100) { tooHigh(); break; }

} while(false);

http://php.net/manual/de/control-structures.do.while.php

not sure how this would work - could you explain it?
Glycerine
What? How does this accomplish the same thing?
Christian Mann
@user488149 I don't think user488149 is your real name either... I bet you its something embarrassing like Bernard or A. Gorrila!
Glycerine
I bet he smells funny too.
Christian Mann
+2  A: 
if($price > 100)
{
  //too high
  tooHigh()
}
elseif($price > 70) //it wasn't greater than 100 - is it greater than 70?
{
  //negotiate
  negotiate()
}
elseif($price > 20) //OK, wasn't greater than 70 OR 100 - greater than 20 then?
{
  //accept
  accept()
}
else //Guess not - just don't do anything
{
  //thank and escape
  thankAndEscape()
}

Case statements can't do conditions unfortunately. They really are 'if this is the case then...' in brutal honesty

This should work as the conditions will simply fall through to the next until it reaches the bottom. if one matches - the rest of the statement is ignored... Thats if I have my logic the right way up...

I don't think you'll need to do the range matching as is already native to the if statement. For example. The statement 'between 70 and 20 ' is simplified to above 20 lower than 70 in seperated conditions. A little more efficient and easier to read.

Glycerine
I'm sure that his code allows conditions, because it will execute the blocks which are boolean true.
Matt
True... True... (ha. See what I did there. O_O)
Glycerine
+1  A: 
if ($price>100)
{
        tooHigh(); 
}
elseif($price<=100) && ($price>70))
{
        negotiate(); 
}
elseif(($price<=70) && ($price>20))
{
        accept(); 
}
elseif ($price<=20)
{
        thankAndEscape(); 
}

looks more compact and readable, doesn'it?

greg0ire
+1  A: 

if ($price <= 20) {
        thankAndEscape(); 
} elseif ($price <= 70) {
        accept();
} elseif ($price <= 100) {
        negotiate(); 
} else {
        tooHigh(); 
}
  1. switch(true) is necessary in a small number of cases; the fact you make the check with expressions inside the case keyword means the syntax does not fit at all, that's a good reason to use the if...elseif construct.
  2. switch...case syntax has higher performances in PHP than if...else in case there's not a default case, so given the solution I suggest (which has a default case) I would not use the switch...case syntax.
  3. By making progressive checks, starting from the lowest price range and increasing it step by step, you don't have to check for the whole range, since the first point of the interval is surely greater than the last of the previous check. The solution I suggest is simpler and solid, giving moreover better performances.

Try it and post the results/your impressions (information about performances are appreciated :)

Cheers!

Emanuele Del Grande
I hate little performance issues. I insist on using foreach over for loop! I think performance can be accelerated much on working logic rather tools.
nerkn
I deeply agree :)
Emanuele Del Grande
switch can have a factor of 50x improvement over an if statement. Many languages have this issue - compiled or otherwise. But sometimes there is no other option. Unless you're going for an condition-less paradigm.
Glycerine
@Emanuale - Course you agree. Your own code is always the greatest...
Glycerine
@Glycerine - I meant I care for performance issues at any level! Code optimization is a typical PHP issue for its nature of interpreted (not compiled) language. Turning to something like Python which also fits good for web programming, or using a PHP compiler may allow you not to worry at all about it. But writing code without care for the low level layer could in any language drive towards unpretty behaviours (e.g. unexpected type casting caused by weak typization), strange errors (e.g. garbage collection in circular references) or unsatisfying performances (e.g. the current thread).
Emanuele Del Grande