3

is it possible to refactor and completely avoid many if else if else statements for better readability. for example :

 function canIWatch(age) {
if (age < 6 && age > 0) return "You are not allowed to watch Deadpool after 6:00pm.";
else if (age >=6 && age < 17) return "You must be accompanied by a guardian who is 21 or older.";
else if (age >=17 && age < 25) return "You are allowed to watch Deadpool, right after you show some ID.";
else if (age >= 25 && age < 50 ) return "Yay! You can watch Deadpool with no strings attached!";
else if (age >= 50) return " You are too old to watch deadpool";
else return "Invalid age."
}

how can i write a much cleaner code without having this much if else statements

Ewomazino Ukah
  • 2,286
  • 4
  • 24
  • 40
  • 2
    You don't need `age >= 25` and all other `>=` second checks. – zerkms Nov 30 '16 at 20:31
  • 5
    Possible duplicate of [Switch on ranges of integers in JavaScript](http://stackoverflow.com/questions/5619832/switch-on-ranges-of-integers-in-javascript) – Geoff James Nov 30 '16 at 20:32
  • 1
    Try out http://codereview.stackexchange.com/! – Chad Nov 30 '16 at 20:34
  • @community: **please don't** close as duplicate. The checked and the second most upvoted answer from the "Switch on ranges of integers in JavaScript" are both terrible and actually an examples on how **NOT** to use `switch` – zerkms Nov 30 '16 at 20:37
  • @zerkms That is not a good reason for declining to close as a duplicate. If you don't like the top answer, then write a comment or a competing answer, or put an upvote/bounty on a better existing answer. – 200_success Nov 30 '16 at 20:54
  • @200_success it's a 5 years old answer with 117 positive upvotes. Nothing would beat it. But just because the question looks the same - it does not mean it must be closed as a dupe (especially when doing so helps spreading awkward practices). I'm not insisting though - do whatever you want (it's *your decision* whether *you* want newbies to see that as a solution or not). I prefer the Nina's answer *here* much more, hence I upvoted it. – zerkms Nov 30 '16 at 20:57
  • @zerkms -- I'm not sure I agree with "spreading awkward practices". I will concur that different people's versions of "readability" can vary so much. But, in programming I would argue that a `switch` statement is (probably - where possible to use) a lot easier to read than a ton of if/else statements chained together. I agree with you on Nina's answer, hence why I upvoted it, also. – Geoff James Nov 30 '16 at 21:00
  • @GeoffJames after writing code for more than 15 years - I struggle to remember at least one case when having the `switch-case` with reversing constant and expression parts was reasonable. But it's just my experience. `switch(true)` under no circumstances would pass my codereview. – zerkms Nov 30 '16 at 21:02
  • 1
    @zerkms - I agree, it's very unorthadox - can't say I've ever had to use it myself, either! :) – Geoff James Nov 30 '16 at 21:04

2 Answers2

6

You could change the check and start with the smallest value and skip the else part, because you return.

function canIWatch(age) {
  if (age < 0) {
      return "Invalid age."
  }
  if (age < 6) {
      return "You are not allowed to watch Deadpool after 6:00pm.";
  }
  if (age < 17) {
      return "You must be accompanied by a guardian who is 21 or older.";
  }
  if (age < 25) {
      return "You are allowed to watch Deadpool, right after you show some ID.";
  }
  if (age < 50) {
      return "Yay! You can watch Deadpool with no strings attached!";
  }
  return "You are too old to watch deadpool";
}

console.log(canIWatch(-1));
console.log(canIWatch(2));
console.log(canIWatch(8));
console.log(canIWatch(20));
console.log(canIWatch(28));
console.log(canIWatch(60));

A different solution would be to use an array with objects for the terms

function canIWatch(age) {
    var terms = [
            { age: 0, text: "Invalid age." },
            { age: 6, text: "You are not allowed to watch Deadpool after 6:00pm." },
            { age: 17, text: "You must be accompanied by a guardian who is 21 or older." },
            { age: 25, text: "You are allowed to watch Deadpool, right after you show some ID." },
            { age: 50, text: "Yay! You can watch Deadpool with no strings attached!" },
            { age: Infinity, text: "You are too old to watch deadpool" },
        ],
        text;

    terms.every(function (a) {
        text = a.text;
        return a.age <= age;
    });
    return text;
}

console.log(canIWatch(-1));
console.log(canIWatch(2));
console.log(canIWatch(8));
console.log(canIWatch(20));
console.log(canIWatch(28));
console.log(canIWatch(60));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

do the fact that you use return you should at least avoid else if but use if only

  function canIWatch(age) {
    if (age < 6 && age > 0) return "You are not allowed to watch Deadpool after 6:00pm.";
    if (age >=6 && age < 17) return "You must be accompanied by a guardian who is 21 or older.";
    if (age >=17 && age < 25) return "You are allowed to watch Deadpool, right after you show some ID.";
    if (age >= 25 && age < 50 ) return "Yay! You can watch Deadpool with no strings attached!";
    if (age >= 50) return " You are too old to watch deadpool"; 
    return "Invalid age."
  }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107