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.

17 comments:

  1. Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!!
    seo company in india
    digital marketing company in india
    seo company in chennai
    digital marketing company in chennai

    ReplyDelete
  2. Thank you for sharing the information here. Its much informative and really i got some valid information. You had posted the amazing article.

    Laser Root Canal Treatment In Chennai

    Best Dental Clinic In Velachery

    ReplyDelete
  3. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

    Digital Marketing Company in Chennai

    ReplyDelete
  4. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete


  5. نقل عفش الكويت رخيص نقل عفش الكويت رخيص
    نقل عفش حولي نقل عفش حولي
    نقل عفش المنطقة العاشرة نقل عفش المنطقة العاشرة
    نقل عفش الجهراء نقل عفش الجهراء
    نقل عفش الفروانية نقل عفش الفروانية
    نقل عفش الكويت نقل عفش الكويت


    ReplyDelete

  6. شركة شحن عفش من الدمام لمصر شركة شحن عفش من الدمام لمصر
    ارخص شركة نقل عفش بالمدينة المنورة ارخص شركة نقل عفش بالمدينة المنورة
    اسعار شركات نقل العفش بالدمام اسعار شركات نقل العفش بالدمام
    شركة شراء اثاث مستعمل بالدمام شركة شراء اثاث مستعمل بالدمام
    شركة شراء اثاث مستعمل بالاحساء شركة شراء اثاث مستعمل بالاحساء
    شركة شحن عفش من المدينة لمصر شركة شحن عفش من المدينة لمصر
    ارخص شركة نقل عفش بمكة ارخص شركة نقل عفش بمكة
    افضل شركة نقل عفش بالاحساء

    ReplyDelete
  7. فيلم سكس قوي وممتع جميله بجسم مشدود في فيلم سكس وجنس واباحه قوي وممتع ويخلي أي زبر يقف شاهد أفضل فيديوهات وافلام الجنس والسكس المجانية اونلاي سكس جديد سكس عربي سكس مصري افلام سكس اجنبي علشان تدلع بتاعك احلى افلام سكس اون لاين مباشر صور سكس محارم قصص سكس حقيقية

    افلام سكس عربي مترجمة افلام سكس 2018 سكس مترجم مصري شاهد افلام سكس اجنبي مترجم تحميل احدث افلام النيك العربية افلام نيك اجنبي مترجمة افلام سكس محارم

    افلام سكس مثيرة لزوج ممحون يرغب في ممارسة الجنس باي طريقة ويحاول اغراء زوجته الجميلة صاحبة الجسم الساخن افلام سكس مثيرة صور سكس ساخنة نيك صور كس

    صور سكس ساخنة نيك كساس بنات بعنف وهى هايجة وكسها مولع صور سكس خلفى نيك كساس بنات هايجة ,صور سكس نار,صور سكس محارم

    ReplyDelete
  8. Nice blog..! I really loved reading through this article. Thanks for sharing such
    a amazing post with us and keep blogging...Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chennai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

    ReplyDelete
  9. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
    Sql server dba online training

    ReplyDelete

  10. encoder
    We are an MRO parts supplier with a very large inventory. We ship parts to all the countries in the world, usually by DHL AIR. You are suggested to make payments online. And we will send you the tracking number once the order is shipped.

    ReplyDelete
  11. Thanks for sharing is so amazing and helpful to us.
    Buy Hydrocodone online

    ReplyDelete

  12. Best Article buy Pain Pills online Excellent post. I appreciate this site. Stick with it! Because the admin of this web page is working, no doubt very quickly it will be well-known, due to its quality contents.This website was how do you say it? Relevant!! Finally, I’ve found something that helped me.
    Best Article buy Roxicodone online Excellent post
    buy Xanax online
    buy Oxycodone online

    Best Article buy Pain Medications online Excellent post. I appreciate this site. Stick with it! Because the admin of this web page is working, no doubt very quickly it will be well-known, due to its quality contents.This website was how do you say it? Relevant!! Finally, I’ve found something that helped me.

    buy Research Chemicals online

    buy Roxicodone online

    buy Cbd Isolate online

    ReplyDelete