Switch Statement Alternative

preview_player
Показать описание
To simplify switch statements using an object literal technique, you can create an object where the keys represent the cases you want to handle, and the values are functions that should be executed for each case.

Here's a step-by-step guide on how to replace switch statements using an object literal technique:

Define an object where the keys correspond to the cases you want to handle.
For each key (case), assign a function or value that should be executed or returned when that case is encountered.
Instead of using a switch statement, you can use the object to look up and execute the appropriate function based on the case.
Here's an example:

// Traditional switch statement
function processFruit(fruit) {
switch (fruit) {
case "apple":
return "A fruit that is red or green.";
case "banana":
return "A fruit that is yellow.";
case "orange":
return "A fruit that is orange.";
default:
return "Unknown fruit.";
}
}

// Object literal
const fruitInfo = {
apple: "A fruit that is red or green.",
banana: "A fruit that is yellow.",
orange: "A fruit that is orange.",
default: "Unknown fruit.",
};

function processFruit(fruit) {
// Use the object literal to look up the information
}

#FlutterMapp
#Flutter
#coding
Рекомендации по теме
Комментарии
Автор

Wow, the comments are so harsh. Programmers can be quite unpredictable, but I love it!

FlutterMapp
Автор

The real takeaways from this video are:
1. There are often multiple ways to do things.
2. Don't always believe people when they tell you that something is the "right" way. Try to understand _why_ something is better or worse.

sb_dunk
Автор

I'm not an expert in javascript, but generally associative maps like that are implemented internally using hashmaps or balanced binary search trees (as the C++ libraries do). In either case, creating these data structures will take extra time and memory, and the way it's presented here, this data structure would need to be created every time the function is called. Especially when there are only 4 conditions like this case, you're much better off using the switch statement

roelyoon
Автор

Enum + switch will compile to a super small and fast jump table

hansdietrich
Автор

For everyone wondering why webs and apps work worse and worse everyday

NullPointer
Автор

thank God, at least there are people in comment section with common sense.

dienvidbriedis
Автор

Why would you put a map inside a function? then it needs to rehash every time the function is called. Put it outside the function

Eric-xhee
Автор

after watching the video, I came to the comments to see him getting roasted. I wasn’t disappointed

Claxiux
Автор

And tomorrow project manager will tell you to add notification after coffee receiving

shalidor
Автор

Great job not explaining why the switch statement is wrong

frackitos
Автор

Enum + Switch is better because when you add new value in enum it automatically shows all places where you need add new value. It better for refactoring and it better for performance.

lukas.pierce
Автор

The reason I like this over the switch statement is it's very easy to add or subtract items from the map object. Not to say I don't use a switch statement, but I've used the object lookup more times.

Imagine if we wanted to add more items to the list of values. The map object could be loaded dynamically from a CSV file that could be edited by somebody else and presto we can add values without ever touching the code.

I've done this method in code for my work with a pricing sheet. Sales people can provide the CSV and presto updated values.

As far as speed, if you can actually notice the difference between the switch statement and the object, you've got other issues. Keep in mind that the object lookup is completely inside the language while the switch statement is something you need to write. Letting the language handle it means it can attempt to optimize what's happening.

rnseby
Автор

If you listen to all of these YouTube channels you begin to develop analysis paralysis. Tools exist to test performance and over time you’ll understand what’s performant over non performant.

SuperKidontheblock
Автор

these kind of videos are one of the reasons why some beginners quit before reaching their goals

dwiputrasam
Автор

Could you please explain what you find cleaner and easier about this approach?

Rose-eche
Автор

you could also return map[type] ?? 'Not Found'
no need for caffeine variable

sivuyilemagutywa
Автор

the fact that this is cleaner, the simpler the better, if you dont mind execution speed, thanks

frcl
Автор

This change is good if youre doing data driven work, since you can dynamically change the values to fit new values, but otherwise using a switch is always superior. Often, compilers have some pretty magical algorithms inside that can optimise switch blocks into very efficient jump tables. Even if you're using an interpreted language, there's almost always a reason that language offers you switch blocks, and its almost always a performance reason

hydroxa
Автор

Man I love your videos. I am a senior engineering and have worked with Dart quite a while ago. Really enjoy the techniques. Keep going and ignore the hate. ❤

shayokhshorfuddin
Автор

I think this may depend when the map is created, the number of keys it holds and the requirement to have one single value to each key. In some situations a single option may require several lines of code leading to more and more branches off each root map.. while that isn't bad per se.. it may start to become less navigable when changes are needed.

Audiencu