2016/10/01

How to easily avoid switch-case statement with function mapping in JavaScript

I won't advertise why it's good to avoid switch-case. While this matter might still be an open dispute I think there are usually better ways to implement what a switch-case does.

My vote would go for polymorphism, unfortunately much JavaScript code out there is not written following good OOP principles. So if you just need to work with given code base but not making a revolution there is a neat way to go:
const actions = {
    value1: actionX,
    value2: actionY,
    value3: actionZ
};

actions[value]();
Where action X, Y, Z are functions.

Of course it's good to add some check for undefined value, like:
actions[value] && actions[value]();

Or an error might be thrown if no mapping found as follows:
const action = actions[value]
        ? actions[value]
        : (value) => throw new Error('No action mapped for value: ' + value);

action(value);
Well, that's it. I believe it helps to reduce clutter in code. Just compare it to a typical switch:
switch (value) {
    case 'value1':
        actionX();
        break;
    case 'value2':
        actionY();
        break;
    case 'value3':
        actionZ();
        break;
    default: throw new Error('No action mapped for value: ' + value)
}


Exactly the same idea can be done in Groovy, Java and other languages if you are not up for polymorphism on a given case. Although for some languages with no functional paradigm, like Java before version 8, you may need to create some sort of Action class definition.

6 comments:

  1. Chennai's No.1 software training institute, Infycle Technologies, provides the best Big Data Hadoop Training in Chennai for students, freshers, and tech professionals along with other corporate courses such as Data Science, Cloud computing, DevOps, Digital Marketing, Python, Big Data, Selenium, Java, Hadoop, iOS, and Android development with 100% hands-on training. After the completion of training, the students will be sent for placement interviews in the core MNC's. Call 7502633633 to get more info and a free demo.Big Data Hadoop Training in Chennai | Infycle Technologies

    ReplyDelete
  2. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. Mason Jacob

    ReplyDelete
  3. Just 99 Web Design offers affordable web design packages for every business, from basic one-page websites to full eCommerce website design services. affordable web development

    ReplyDelete
  4. Anonymous8/12/22 13:06

    U&G research has its foundations the structural–functionalist systems strategy to discerning the interaction between living entities and their context . Diverging from other media consumption theories, U&G identifies the needs and desires people have to use a selected media . Choices as to what stage of engagement the person has with media are made based mostly upon the perceived psychological needs that the content of a media is prone to fulfil . Thus, U&G posits that media consumption behavior is freely guided by psychological gratifications derived from utilizing the product nicely as|in addition to} practical reasons to 카지노사이트 use it. Additionally, the results of the individual’s media use could be largely explained by understanding the individual’s objective for utilizing the media . While we absolutely love the cell functionality of BetOnline, it is worth mentioning that the casino element is not out there through their cell platform.

    ReplyDelete