5

I have a horrible nested if. There could in the future be even more lines.

if (people < 10) {
    price = 500;
} else if (people >= 10 && people < 25) {
    price = 350;
} else if (people >= 25 && people < 100) {
    price = 250;
} else if (people >= 100) {
    price = 200;
}

The price goes down as the volume goes up. How do I refactor this to make it more maintainable/readable?

Edit: I tried a switch and it was not any better?

3
  • Yes sorry a typo. Fixed. Commented Nov 8, 2018 at 10:07
  • I tried a switch.. And why didn't you like it? Commented Nov 8, 2018 at 10:09
  • 1
    Passing (true) into it seemed a bit hacky, plus there was little difference to the nested if. Commented Nov 8, 2018 at 19:07

5 Answers 5

4

One option would be to use an array that defines the thresholds, then .find the appropriate value in the array. This will be very concise, especially when there are lots of thresholds:

const thresholds = [
  [100, 200], // need 100+ people for the price to be 200
  [25, 250], // else need 25+ people for the price to be 250
  [10, 350],
  [0, 500]
];
function findPrice(people) {
  return thresholds.find(([limit]) => people >= limit)[1];
}

console.log(findPrice(53)); // 53 people
console.log(findPrice(25));
console.log(findPrice(24));

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

2 Comments

Not at all, unless you have something like thousands of thresholds and are running the function hundreds of times per second, or something like that. Code readability and conciseness is more important in 99% of situations.
@AlexanderDavidson I think that the performance you loss is worth the maintainability of it. Now that you have an object iteration, you can easily add new treatment or new threshold. I would 100% always prefer object iteration to plain if/else statements
3

You could take a function with early exit. The previous check is the condition for the next check or for getting the maximum result.

The advantage is to prevent chains of else ifstatements and to offer a better maintanability.

function getPrice(people) {
    if (people < 10) {
        return 500;
    } 
    if (people < 25) {
        return 350;
    }
    if (people < 100) {
        return 250;
    }
    return 200;
}

var price = getPrice(people);

More to read:

1 Comment

Can you please elaborate on your answer? How returning in separate if block is better than else if's?
1

Well you dont need the check for >=, when the check will stay in this form:

if (people < 10) {
    price = 500; 
} else if (people < 25) { 
    price = 350;
} else if (people < 100) { 
    price = 250; 
} else { 
    //people count is implicitly greater than 100
    price = 200; 
}

On each (next) step the people count is implicitly greater than the previous check, so eg. if people < 10 results in false the value is implicitly greater than 9 or >= 10. For this reason the duplicate check is not needed an thus can be omitted.

Comments

0

function applyConf(v) {
  return [{
    // false means infinite
    min: false,
    max: 9,
    value: 500,
  }, {
    min: 10,
    max: 24,
    value: 350,
  }, {
    min: 25,
    max: 99,
    value: 250,
  }, {
    min: 100,
    max: false,
    value: 200,
  }].find(({
    min,
    max,
  }) => (min === false || v >= min) && (max === false || v <= max)).value;
}

console.log(applyConf(-10));
console.log(applyConf(8));
console.log(applyConf(20));
console.log(applyConf(80));
console.log(applyConf(100));
console.log(applyConf(100000));

Comments

0

I would prefer instead of to many if to use switch condition as below

function getPrice(people)
{
    switch(true){
        case people<10: return 500;
        case people<25: return 350;
        case people<100: return 250;
        default: return 200;
    }

}

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.