STOP Using Switch Statements! Use These Instead...

preview_player
Показать описание


STOP Using Switch Statements! Use These Instead...

(00:00) Intro
(00:05) Welcome
(00:18) Switch Statements
(01:34) Lookup Objects (or object lookups)
(02:58) How to have a default value
(03:55) ES6 Maps
(05:40) Review
(06:14) Maps vs Objects

Reference:
MDN Maps vs Objects:

✅ Follow Me:

Was this tutorial about avoiding switch statements and instead using object lookups or Map objects helpful? If so, please share. Let me know your thoughts in the comments.

#stop #switch #statements
Рекомендации по теме
Комментарии
Автор

I'm not sure if this is mentioned in the video, but using maps/objects is even more efficient as key lookup is O(1) while a switch-case is O(n) where n is the amount of cases before a match is found.

(Edit): switch-cases are implemented using jump tables, which result in O(1) hence both methods would be the same in terms of performance.

arshiagholami
Автор

You can instantiate a map with values as well .. new Map([ ['css', 'text/css'], ['js', 'text/js'] ])

AmxCsifier
Автор

I feel like this is a bit deceptive... Switch statements are a very useful construct. It is good to use something like a lookup object instead if you are simply using the switch to select a value, but switches can more correctly be used to run separate code depending on the switched value. So, rather than "Stop using switch statements", this seems like more of a "Stop doing this with switch statements"...

superkaeldev
Автор

man I was struggling wth assigning values to cards ( like poker cards.. JACK = 11, QUEEN = 12, KING = 13, ACE = 14) but your video solved it !!! LOVE IT THANK YOU !!! 🙏🙏🙏

mattfly
Автор

simple and to the point, thank you dave :)

a_maxed_out_handle_of__chars
Автор

Using the object or Map reference you now have to remember in every case you use it to define the same default/fallback value after your usage. What happens if the default/fallback needs to change? That would require changes everywhere you have referenced it. That is more of a smell than having a default value defined in one place in my opinion.

du_v
Автор

All of these examples assume that the switch is basically just returning a string. But often there is a lot of programming logic in each state, then your methods fail.

bloodshoot
Автор

Wow amazing, i will try this on react conditional rendering

ougininja
Автор

Cool! I'm so stupid to realize that I can use an object and get the code I need! Though I'm thinking of an array, but this is a great way to work with multiple mapping!

thesatirist
Автор

Awesom, Thank you for amazing contents 👍👍

코린이세
Автор

Great video as usual. Thanks for sharing

josejuanmoninocoll
Автор

you can convert a object to map: const objToMap = (o) => new Map(Object.entries(o));

rtorcato
Автор

Very useful content, thank you so much!

gustavokuze
Автор

Thank u Sir New to your Channel, will be Go back Looking at more of your Video's, Take Care Mate

NSWMods
Автор

Thanks for this content. I like these shorter vedios with tips and tricks. I would like to see more of these.

InsEngineered
Автор

Thank you for the great video.

Wouldn't it be more safe to use ' ?? ' instead of the ' || ' ?

ItsGentleChris
Автор

Great video!! What if I want to run a function in my value rather than a string? I have this:

Object.entries(SpectrumObj).forEach(([key, val]) => {
const myMap = new Map()
myMap.set('Spectrum', handleSpectrum(val))
myMap.set('SpectrumNumber', handleSpectrumNumber(val))
myMap.set('RadioFrequency', handleRadioFrequency(val))

myMap.get(key)
}

can't figure out how to run the function other than turning the values into strings and running an eval() on them

HUE
Автор

I think the first option is better because in the second one we need to declare or set multiple maps.

ercntreras
Автор

As a veteran web developer, this is excellent! I'm really looking forward to putting it into use tomorrow.

kxmode
Автор

Great Video again, but this example seems very specific here, is it possible to use Map instead of switch in other conditions like the reducer function in redux?

chetanjain