简体   繁体   中英

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

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

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)); 

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."
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM