DEV Community

Replace your switch statement and multiple "if and else", using Object Literals - [en-US].

Tauan Tathiell on November 27, 2019

So First, What is Switch Statement? A switch is a function that receives data, and that data will be analyzed, if this data is equal t...
Collapse
 
kibobishtrudelz profile image
Petar Kolev

From syntactic point of view ternary is way more terrible to read instead of switch :d

Collapse
 
deathshadow60 profile image
deathshadow60

Most of your arguments against SWITCH are in fact it's features. It's like the derpy linters that throw a hissy-fit if you drop-through, one of the entire reasons to use switch in the first place. Oh noes, you have to say break, return, or continue, notz thatz. :/

Though I'm often dismayed how often I see break; after a return; (hurr-durrz) or how many people don't realize continue can also be used there.

From an efficiency standpoint, your claims of disadvantages of switch are more myth than fact. JavaScript table lookups are still going to do comparisons, you are manually coding such a comparison so if anything is going to be slower in execution, it's probably your (Users[type] || Users.default) given how piss poor the performance of JS object indexes are. JUST like the painfully bad performance of JS' ALLEGED arrays. (that are in fact pointered lists).

That said, it's often better to use the object approach you mention for code clarity or organization sake, it's an obvious answer in a lot of situations, but comparing it to IF or SWITCH? Yeah, no.

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Thanks for ur review.. i wrote this article to pass, what i'm leraning .. how i said "//I'm not dictating the rule - it's just another way of solving our everyday problems" ... i don't have 40 years in programming .. u have more arguments then me , i know it .. i was trying to share something cool .. that i learned .. i'm sorry about that :D

Collapse
 
seanmclem profile image
Seanmclem

I like your approach better, and agree with your logic for using it. keep it up

Thread Thread
 
tauantcamargo profile image
Tauan Tathiell

Thanks man ... let's keep sharing knowledge...

Collapse
 
nikitahl profile image
Nikita Hlopov

Nice article and interesting approach. Thanks!
I have a question however. Why would you use methods to return the string instead of assigning this string to a property like so:

const Users = {
  admin: 'This User is Admin!',
  client: 'This User is Client!',
  salesman: 'This User is Salesman!',
  default: 'This User is MasterUser'
}

I mean wouldn't it be more clean and straightforward way?

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Hey man ... thanks .. so in the project that i'm current working i used methods to change the data and other things ... it was just a mistake hehehe i'm sorry .. your approach is absolutelly correct... thanks for your review i'm going to edit it ..

Collapse
 
akolybelnikov profile image
Andrey Kolybelnikov

I was about to comment with the same when I saw this has already been suggested :)
One more cool little enhancement will allow you to adhere to immutability principle (that is if you'd like to prevent anyone from changing your object properties somewhere in the code later):

Object.freeze(Users)

From now on your object literal is readonly.

Collapse
 
tauantcamargo profile image
Tauan Tathiell

hey man thanks for the comment :D

Collapse
 
dance2die profile image
Sung M. Kim

Thanks for sharing the post in both English & Brazilian Portuguese (pr-BR).

For those who want to read pr-BR version, check out this post.

Collapse
 
tauantcamargo profile image
Tauan Tathiell • Edited

Thanks man... I'm going to do that for all my articles .. i'm trying to share to our community, everything i'm learning :D

Collapse
 
dance2die profile image
Sung M. Kim

You're welcome and I appreciate sharing your knowledge/experience :)

 
tauantcamargo profile image
Tauan Tathiell

Thanks man .. i liked a lot your approach...
// I'm not dictating the rule - it's just another way of solving our everyday problems. hehehehe

 
kibobishtrudelz profile image
Petar Kolev

Yes, I do because in my work’s project we have many ternary operators used and when they are long it’s brain damaging while reading them. Again - just from synthetic point of view :)

Collapse
 
tauantcamargo profile image
Tauan Tathiell

thanks for ur review, i put this default user just to show a sample how to use. but i understand ur point .. i'll change some code's soon... one more time Thanks xD

Collapse
 
juancarlospaco profile image
Juan Carlos

I think this should be benchmarked to prove that this construct is better than the switch,
my guess is it is not.

Collapse
 
tauantcamargo profile image
Tauan Tathiell • Edited

I take this printscreen by benchmark test ...

altimage

Collapse
 
obsessiveprogrammer profile image
Tom Bonanno

Very cool. Thanks for sharing. One issue I can see that you should be aware of is that you won't get an accurate code coverage result for testing, as it will not be seen as branches.

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Thanks for your review man .. i'll update this article or create another to update with new things

Collapse
 
kibobishtrudelz profile image
Petar Kolev

This will be pretty neat if you have super long switch in React reducer for instance =] Thanks for the article! It’s nice to know another way of doing things!

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Yes man .. i like this approach .. i dont guess this will solve all my problems but is a cool approach... thanks for your review

Collapse
 
kulpras profile image
Prasanna Kulkarni

I liked the way you explained. Will use object literals in my future projects.

Collapse
 
tauantcamargo profile image
Tauan Tathiell

Hey man appreciate that.. thanks a lot