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 if you compare it to something like:
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 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.
AWS Training in Bangalore - Live Online & Classroom
ReplyDeletemyTectra 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.
As I read the blog I felt a tug on the heartstrings. it exhibits how much effort has been put into this.
DeleteFinal Year Project Domains for Information Technology
Spring Training in Chennai
Project Centers in Chennai for CSE
JavaScript Training in Chennai
ReplyDeleteشركة تنظيف خزانات بالمدينة المنورة
أفضل شركة تنظيف بالمدينة المنورة أفضل شركة تنظيف بالمدينة المنورة
أفضل شركة مكافحة حشرات بالمدينة المنورة أفضل شركة مكافحة حشرات بالمدينة المنورة
شركة غسيل شقق بالمدينة المنورة شركة غسيل شقق بالمدينة المنورة
ReplyDeleteشركة شحن عفش من الرياض لمصر شركة شحن عفش من الرياض لمصر
شركة نقل عفش شرق الرياض شركة نقل عفش شرق الرياض
افضل شركة نقل عفش بجدة افضل شركة نقل عفش بجدة
ارخص شركة نقل عفش بالرياض
شركة نقل عفش من الرياض الى دبي
ارخص شركة نقل اثاث بالدمام ارخص شركة نقل اثاث بالدمام
افضل شركة نقل اثاث بالرياض افضل شركة نقل اثاث بالرياض
شركة نقل عفش
Nice post
ReplyDeletejava training in Marathahalli
spring training in Marathahalli
java training institute in Marathahalli
spring and hibernate training in Marathahalli
DJ gigs London, DJ agency UK
ReplyDeleteDj Required has been setup by a mixed group of London’s finest Dj’s, a top photographer and cameraman. Together we take on Dj’s, Photographers and Cameramen with skills and the ability required to entertain and provide the best quality service and end product. We supply Bars, Clubs and Pubs with Dj’s, Photographers, and Cameramen. We also supply for private hire and other Occasions. Our Dj’s, Photographers and Cameramen of your choice, we have handpicked the people we work with
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteSql server dba online training
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog.
ReplyDeleteSelenium training in Chennai | Devops online training
It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
ReplyDeleteDocker online training
Docker certification training
Docker online course
Docker training course