0

What is the best way to optimize this code? I know that if and else statements will be faster, but I want something that will be clean and concise. Any ideas?

switch ($data['months']) {
case ($data['months'] >= 400):
    $data['months'] = 400;
    break;
case ($data['months'] >= 360):
    $data['months'] = 360;
    break;
case ($data['months'] >= 60):
    $data['months'] = 60;
    break;
case ($data['months'] >= 48):
    $data['months'] = 48;
    break;
case ($data['months'] >= 36):
    $data['months'] = 36;
    break;
case ($data['months'] >= 24):
    $data['months'] = 24;
    break;
case ($data['months'] >= 12):
    $data['months'] = 12;
    break;
case ($data['months'] >= 9):
    $data['months'] = 9;
    break;
case ($data['months'] >= 6):
    $data['months'] = 6;
    break;
case ($data['months'] >= 3):
    $data['months'] = 3;
    break;
case ($data['months'] >= 1):
    $data['months'] = 1;
    break;
default:
    $data['months'] = 12;
}
1

3 Answers 3

5

I guess you could use foreach with an array of breakpoints.

$breakpoints = array(400, 360, 60, 48, 36, 24, 12, 9, 6, 3, 1);

// Loop through each breakpoints
foreach($breakpoints as $breakpoint){

    // Proceed to next breakpoint
    if($data['months'] < $breakpoint)
        continue;

    // No need to loop through the rest of the array
    $data['months'] = $breakpoint;
    break;

}

// Handle the cases where $data['month'] is less than 1
if($data['months'] < 1)
    $data['months'] = 12;

Update

An alternative would be checking if the $data['months'] is less than 1 before the loop:

if($data['months'] < 1)
    $data['months'] = 12;
else{

    $breakpoints = array(400, 360, 60, 48, 36, 24, 12, 9, 6, 3, 1);

    // Loop through each breakpoints
    foreach($breakpoints as $breakpoint){

        // Proceed to next breakpoint
        if($data['months'] < $breakpoint)
            continue;

        // No need to loop through the rest of the array
        $data['months'] = $breakpoint;
        break;

    }

}
Sign up to request clarification or add additional context in comments.

8 Comments

Though you were first to respond, I chose the next answer since the setting of the default value was much cleaner and eliminated an additional conditional check.
@Bryce True, but it's looping through each breakpoints and reassigning the $data['months'] even though it doesn't need to...
Good point, I think I'll take mix of them both. haha. Thanks!
@Bryce I just noticed that the way @ceejayoz did it, no matter your initial value of $data['months']... after the loop, it's going to be 12. Since it's overwriting the initial value before the foreach.
Yup, just realized this as well, hence why I have marked yours as the correct answer :).
|
5

Something like this should do the trick.

$breakpoints = [1, 3, 6, 9, 12, 24, 36, 48, 60, 360, 400];

foreach($breakpoints as $breakpoint) {
    if($data['months'] >= $breakpoint) {
        $value = $breakpoint;
    }
}

if(!isset($value)) { $value = 12; }

3 Comments

I'd note, though, that your default in the switch doesn't make sense to me. $data['months'] is never gonna be 1 in your code - I kept that weirdness.
I don't think this will work because you're assigning the $data['months'] to 12 by default. So after your loop, no matter the initial value, the $data['months'] will be equal to 12.
@ChinLeung Yeah, you're right. Brain's not working great today.
0

Here is what I ended up with in the end.

$breakpoints = [400, 360, 240, 120, 60, 48, 36, 24, 12, 9, 6, 3, 1, null];
foreach($breakpoints as $breakpoint){
    if($data['months'] >= $breakpoint) {
        $data['months'] = $breakpoint ? $breakpoint : 12;
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.