Escape If-else hell in Javascript

Backstory / Drawback

Few months in the past, there’s a sure case the place I must calculate the proportion of enter file in every type that person has stuffed (It was for a react native app that take person suggestions by filling a number of type that characterize totally different class corresponding to private info type, the person property info, and so on.), the system circulation seem like this in a nutshell.

The primary method was utilizing if/else assertion to deal with the conditional logic. Though it is perhaps a good suggestion for one or two circumstances right here and there, utilizing a number of if-else statements chained collectively will make your code look very ugly, and fewer readable, and for my case there may be most likely greater than 30 if-else statements in scattered in 5 totally different varieties. Not gonna lie, it look quite simple and straight to the purpose, but painful to learn.

Additionally, when my peer reviewing the PR, he refers one thing humorous in reddit about the code behind yandere simulator
Yandere Simulator code

As you may see, it’s a hell of if-else statements.

The answer

The answer shall be fluctuate, relies on your case / want. However most probably the factor that you simply want is object. As as an example, for instance it’s worthwhile to return a string primarily based on a key

operate checkStatus(standing) {
    if (standing.toLowerCase() === 'accessible') {
        return `The person is presently accessible`
    } else if (standing.toLowerCase() === 'busy') {
        return `The person is presently busy`
    } else if (standing.toLowerCase() === 'away') {
        return `The person is away from keyboard`
    } else if (standing.toLowerCase() === 'breaktime') {
        return `The person is having a great lunch`
Enter fullscreen mode

Exit fullscreen mode

Simply think about if in case you have different 20+ standing sort ? Will you be comfy studying or writing that a lot line of if-else statements?

As an alternative we will use object or Map object to make a type of desk include paired key and worth to look as much as.

operate checkStatus(standing){
const statusList = {
   accessible: 'The person is presently accessible',
   busy: 'The person is presently busy',
   away: 'The person is presently away from keyboard',
   breaktime: 'The person is presently having a great lunc'

return statusList[status]; //console.log(statusList[status])
Enter fullscreen mode

Exit fullscreen mode

This may be additionally utilized in algorithm leetcode-type-of-question to avoid wasting you up a while from writing repeated if-else assertion again and again.

Thanks for studying!!!
Have a great day, and do not forget that challenge you at all times take into consideration will not code itself 🤪.

Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.

Checkout extra Articles on Sayed.CYou

#Escape #Ifelse #hell #Javascript