"Switch Statements" are Bad. Do This Instead.

  Рет қаралды 168,052

Flutter Mapp

Flutter Mapp

Күн бұрын

Пікірлер: 345
@KanashimiMusic
@KanashimiMusic 2 жыл бұрын
Just some general advice for any programmers that see this comment: When anybody tells you "NEVER do this in programming", they're in most cases over-simplifying and generalizing, OR they don't know enough about the topic in the first place. For example, a while ago I watched a video saying that you should never use "else" statements and instead work with other options like guard clauses and whatnot. While that is true in many cases and that video helped me make my code a lot more readable, there are just a few cases where strictly not using "else" just makes it unnecessarily complicated for everyone, both you and readers of your code. EVERYTHING in programming has its use, no matter what anybody else tells you. Sometimes those use cases are rather niche, sometimes they aren't. Even stuff like "goto", which is almost always a terrible idea, has some very rare cases where it might just make the code easier to understand than whatever the alternative is. This video also only goes over a single way that switch statements are often used: to return a pre-determined value based on another value, and I agree that switch statements might not be the best option for this. Though, a few programming languages like C# and Java have "switch EXPRESSIONS" for this specific purpose which are in fact very readable since they look similar to a typical map syntax. However, switch statements have LOTS of other purposes that can sometimes be a pain to do differently. So saying that "switch statements are generally bad" and "you should never use them" is just completely wrong. It should be saying "don't use switch statements like this" or something. Some additional comments to the video: 1. The switch-case shown in the beginning is written in an unnecessarily complicated way... You could just put a return into each of the cases. That would almost cut the length of that function in half already, since you wouldn't need the variable, the assignments would be replaced by the returns, the breaks would not be necessary anymore, and the return at the end would also not be needed. 2. If you have a map that will be reused over and over again, it should be declared outside of the function and made readonly if you want to make sure (since I think this is JavaScript, Objects.freeze(map) will do the trick here).
@CryMore13
@CryMore13 2 жыл бұрын
not js, but good comment
@KanashimiMusic
@KanashimiMusic 2 жыл бұрын
@@CryMore13 Thank you :) Also, what is the language used in the video, if it's not JS? Because to me it looks 100% like JS code 🤔
@ivanbilinchuk9155
@ivanbilinchuk9155 2 жыл бұрын
@@KanashimiMusic dart
@KanashimiMusic
@KanashimiMusic 2 жыл бұрын
@@ivanbilinchuk9155 Ah alright, I've actually never heard of that. Thanks :)
@CryMore13
@CryMore13 2 жыл бұрын
@@KanashimiMusic you can see on 0:17 that is not JS because of the variable declaration "String Caffeine;"
@Cypekeh
@Cypekeh Жыл бұрын
Switch statements are not "bad" and in many cases they're great, but in this particular example you're right it's easier to use lookup tables
@mitaka_78
@mitaka_78 Жыл бұрын
Also memory wise is more efficient, one is just doing some checks, another is instantiating an object, iterating through it to find an item
@farhanaditya2647
@farhanaditya2647 Жыл бұрын
@@mitaka_78 There is no iteration going on here. You just plug in a key and see if the key is associated with a value.
@mitaka_78
@mitaka_78 Жыл бұрын
​@@farhanaditya2647 In the backend it definetely iterates, whatever code that manages maps in javascript needs to iterate through the map to get a value
@gamerk316
@gamerk316 Жыл бұрын
@@farhanaditya2647 I'm fairly certain it iterates in the backend, it's just invisible to the user. I'd also wager it's significantly slower then using a switch statement, which "could" matter depending on how it's used.
@augustaseptemberova5664
@augustaseptemberova5664 Жыл бұрын
@@mitaka_78 Switch optimally has runtime complexity of O(1), at worst O(n) but only for really really large n. If the map you're using is a HashMap, like in Java, its runtime complexity for lookup is O(1), i.e. the same as a switch. If the map you're using is a binary tree, like in cpp, the runtime complexity for lookup is O(n), because here acutal iteration takes place. If it's a binary search tree, it's O(log n). In the end it really depends what programming language you're using and what underlying structure is used for the map. Switches are safe bet, because their implementation doesn't vary between programming languages as to affect the performance, and it's the most memory efficient option. In addition, it allows easy implementation of "fall-through" behavior. In Java however, a HashMap will usually be as performant as a switch, and might even be faster depending on how you use it. This again depends on what your JIT does with your Map or Switch. There really are no easy answers here. The safest bet is try both, check their performance and then go with the faster option.
@chudchadanstud
@chudchadanstud 2 жыл бұрын
Nope. Switch cases are better than OOP as they jump directly to the place in memory without wasting cpu cycles. They also don't force you into convoluted design patterns making your code not only less performant but harder to reason about with. Your technique with the map wastes cpu cycles. If you know how maps work under the hood, you'd know why it's a bad idea.
@filiecs3
@filiecs3 Жыл бұрын
Exactly. The map requires dynamically allocating memory and hashing the input on every check. The switch never leaves the stack and is a simple jump table.
@wlockuz4467
@wlockuz4467 Жыл бұрын
Another useful feature of switch case is fall through, its when you want multiple input values to hit the same case/block, switch makes it really clean.
@olavodias
@olavodias Жыл бұрын
When I saw the video I thought: “what am I missing? This seems cool, but in reality it’s actually worse from cpu cycles and memory management perspective”. I’m glad I wasn’t the only one who thought about that.
@wimhuizinga
@wimhuizinga Жыл бұрын
First thing that came to my mind too. Switch case is the most efficient way to do this. Maps require hash and equals to find the right place in memory. That wastes CPU cycles. But this will only become a problem when the code is used heavily. If it's not often called, one could use this for code clarity. Still I would advise to use the switch case instead because everyone knows what the program is about to do when they see a switch-statement and you never know if the code will be hit harder in the future. Perhaps make the switch-statement more readable by making oneliner cases (if your company coding policy allows it) or use modern language features.
@e-jarod4110
@e-jarod4110 Жыл бұрын
In a Dart perpective yes, but this is a "general purpose" technique As a ts dev I find it awesome
@JustinHefley
@JustinHefley 2 жыл бұрын
I think the enhanced enum is a better choice. It is type safe and guarantees you cover all options. You could also return objects so if you wanted to store more than just caffeine, like vitamins, or flavor, you could just add that to the object without refactoring everything.
@rydmike
@rydmike 2 жыл бұрын
Agreed with all you said, even without the new enhanced enum features, just plain old Dart enum would be better, like you said, type safe, ensures you covers all the options, refactoring becomes a breeze. Then go further with enhanced enums and objects. This is actually not so good advice/example, sure maps have their usage too, not saying that 😀💙
@hyungtaecf
@hyungtaecf 2 жыл бұрын
But you have to create it outside of the block of code which is not good for readability…
@JustinHefley
@JustinHefley 2 жыл бұрын
@@hyungtaecf That is the point. You could then reuse it anywhere you want and not just in that one piece of code. If you wanted to reuse the map in other parts of the codebase instead of this one function you’d need to define it separately anyways.
@JustinHefley
@JustinHefley 2 жыл бұрын
@@rydmike I’ve been enjoying flex color scheme. Strong work 💪
@rydmike
@rydmike 2 жыл бұрын
@@JustinHefley Thanks, that's nice to hear 😃🙏💙
@metafates
@metafates 2 жыл бұрын
Please, don’t. This is very inefficient. From the stackoverflow “Switches will always be as fast as if not faster than hash maps. Switch statements are transformed into direct lookup tables. In the case of Integer values (ints, enums, shorts, longs) it is a direct lookup/jmp to the statement. There is no additional hashing that needs to happen. In the case of a String, it precomputes the string hash for the case statements and uses the input String's hashcode to determine where to jump. In the case of collision, it does an if/else chain. Now you might think "This is the same as HashMap, right?" But that isn't true. The hash code for the lookup is computed at compile time and it isn't reduced based on the number of elements (lower chance of collision).“
@pokefreak2112
@pokefreak2112 Жыл бұрын
Implementation will vary from language to language, but this is generally correct. Also if you ever find yourself using switch case on a string there's a decent chance it should actually be an enum.
@phantombeing3015
@phantombeing3015 Жыл бұрын
There is no need to ever worry about such cases. Focus on readability. If you need optimization, go for it after you identify problems.
@luxraider5384
@luxraider5384 Жыл бұрын
@@squishy-tomato no wonder new app take up so much ressources...
@youtubeenjoyer1743
@youtubeenjoyer1743 11 ай бұрын
​@@luxraider5384 Don't worry about it, next year we will have a new iThing with a 100% faster CPU so you won't notice the lag when scrolling down a list of text boxes :)
@nanonkay5669
@nanonkay5669 2 жыл бұрын
For this example, yes a switch statement seems overkill. But when you need to do some computational logic before returning something, or maybe you don't want to return at all, absolutely use a switch statement. In some situations, it makes sense to. It's simply false to say "SWITCH STATEMENT ARE BAD" because some situations require it.
@chrisakaschulbus4903
@chrisakaschulbus4903 Жыл бұрын
Exactly. At first i was interested but then reralized it was about a very specific case.
@gamerk316
@gamerk316 Жыл бұрын
Two points: 1: While not the greatest for readability or code quality, C style switch statements are FAST, and in a performance intensive environment, that could make a measurable difference depending on the amount used throughout the code. 2: If you want to do any type of conditional logic based on the compared value, you may as well just use a switch in any case, as you will need to do the check on the returned value anyways.
@ern0plus4
@ern0plus4 2 жыл бұрын
I'll be thrown out of window (y'know the meme). switch (number) { case 1: return "one"; case 2: return "two"; } return "many"; Same number of lines. Works with almost all C-like languages. No extra memory required for container.
@FlutterMapp
@FlutterMapp 2 жыл бұрын
😂😂 funny meme 😂 Obviously this video is a little bit over exaggerated. It's cool to know some funky ways to replace the typical Switch statements tho 👏
@CrackThrough
@CrackThrough 2 жыл бұрын
@@FlutterMapp did you know in c# you could use switch like: return switch EXP { Cond1 => true, Cond2 => false Cond3 ^ Cond4 => true }
@augustaseptemberova5664
@augustaseptemberova5664 Жыл бұрын
If you do this in c-type languages, you trade O(1) for O(n) or O(log n) (av. lookup runtime complexity of binary tree, and binary search tree respectively). A hashmap would be the only option I know that can compete with switches in terms of look-up speed. Safest option is always: try all options and use the best one. And by try I mean: also try server-side and client-side performance, if you use a programming language that uses JIT.
@pauldpoulpe280
@pauldpoulpe280 2 жыл бұрын
IMO, there's nothing wrong with the switch case method, since it's much more flexible. The map approach is indeed cleaner if we just want to return a value response, but it's not going to work for anything that's more complicated like we have to do some evaluation/calculation/etc. In any case, it's a good advice nonetheless. Just my $0.02
@FlutterMapp
@FlutterMapp 2 жыл бұрын
This is a great point I should have mentions, my mistake. Thanks for sharing Paul 🔥🙏
@snesmocha
@snesmocha Жыл бұрын
@@FlutterMapp bro, do you know how much more memory hash maps use??? a switch statement will always be faster and more efficient
@fabianramirez3222
@fabianramirez3222 Жыл бұрын
Using object literals instead of switch case blocks using logic instead of simple logic for languages like JS is quite simple too, as you can encapsulate each block logic in a function and assign it as object literal values, then, simply invoke the matching key function.
@alexartigas9181
@alexartigas9181 2 жыл бұрын
There was no need for the caffeine variable though, you could return inside the switch in each case and that'd be like half the code anyways... 😅
@FlutterMapp
@FlutterMapp 2 жыл бұрын
true 😂 That could have been even more refactor, dammit 😂👏
@Ohmriginal722
@Ohmriginal722 Жыл бұрын
This makes sense unless you’re writing C code where you’d have to build your own map struct and put it in an array and it wouldn’t be nearly as visually appealing
@bloodyping9644
@bloodyping9644 Жыл бұрын
switch allow you to choose not to break a case, which map doesn’t. Such as this sample: const num = 3 switch (num) { case 3: console.log(`${num} is larger than 2`); case 2: console.log(`${num} is larger than 1`); case 1: console.log(`${num} is larger than 0`); break; } When this is run, it will print out all 3 lines of string to the console, since we didn’t break the upper ones. This could be useful in some situation.
@HomeofLawboy
@HomeofLawboy Жыл бұрын
My MO is: if I want different functionality I use switch case, if I just want data I use a table/map that just stores the data
@deang5622
@deang5622 Жыл бұрын
The code is more compact but takes longer to execute because the CPU has to run through an algorithm to find the text string within the dictionary. What's more important, CPU time, or your time scrolling down a few extra lines of code? The moral of the story is: *ALWAYS* think about the run time performance of the code you write, and decide if it matters to your application or not.
@luxraider5384
@luxraider5384 Жыл бұрын
you seem to have no clue of how hash tables work....
@Christopher_126
@Christopher_126 2 жыл бұрын
Strong disagree with switch statements being bad. Imagine you use an object literal mapping, and then realize down the road you need logic to be executed within one or more of the switch cases. What then? Switch/case is designed exactly for the scenario you gave here and is almost objectively "easier" to read because it's literally telling you "When the case is that a variable is X, execute the below code." Which is more syntactically readable than chaining if statements.
@TechBuddy_
@TechBuddy_ 2 жыл бұрын
This is a great tip but I'd recommend creating a constant map outside the function if it is independent of function inputs also enhanced enums can be used but for simple use cases it makes the code less readable and unnecessaryly complex
@vladarskopin3314
@vladarskopin3314 2 жыл бұрын
I like your advice Tech buddy. I also suspect that this will be a much better option in terms of performance.
@Rudxain
@Rudxain 2 жыл бұрын
I prefer declaring vars only within the scopes they're used, even if they're const. And if the only scope available is global, I use block-scoping to limit the lifetime of the var. This makes it easier for garbage collectors to reduce memory use, and adds context to names (reducing the need for long names), it also reduces the cognitive load because there's less vars to work with, and reduces scope/namespace pollution. Of course, the downside is deeper indentation. I wish there was a lang where var scoping didn't require indentation to make it readable. Rust has the `drop` fn, but it only works on heap-allocated stuff, not stack frames
@Erlisch1337
@Erlisch1337 Жыл бұрын
@@Rudxain sounds instead like you'd possibly increase the amount of GC runs and reducing performance instead. Which is ok I guess depending on what kind of program you are doing
@Rudxain
@Rudxain Жыл бұрын
@@Erlisch1337 True. Sometimes it's ok to declare constants outside of a function, if the constant is heap-allocated and contains more than 256 bytes of data. This is specially useful if the constant is required by more than 1 fn, and guarantees that only 1 allocation is performed (instead of relying on the optimizer)
@khoadoan8966
@khoadoan8966 2 жыл бұрын
switch case speed is a far more efficient approach, with really huge speed difference
@johnwellbelove148
@johnwellbelove148 Жыл бұрын
On a resource limited embedded platform the switch/case form would be preferable due to the much smaller code and data generated by the compiler, compared to a std::map with its internal binary tree implementation. A switch/case will often be implemented by the compiler as a simple jump table.
@graycat9567
@graycat9567 Жыл бұрын
switch statement is not bad. code readability always rely on your current architecture and structure. in your sample it looks clean because the process is simple, but when we talk about different problems like an OOP model that follows DDD event mutations then definitely hash map will not work at all times, we need the switch statement to make the code more readable. eg. abstract IDEvent class ShipmentCreated extends IDEvent class ShipmentDeparted extends .. class ShipmentArrived extends .. ... etc events class Shipment // private fields ... void apply(IDevent e) { switch(e) { case ShipmentCreated: // mutate private fields break; case ShipmentDeparted: // mutate private fields break; case ShipmentArrived: // mutate private fields break; default: throw NotSupported(e); } }
@GeorgeFosberry
@GeorgeFosberry 2 жыл бұрын
"The CIA wants you to think that the switch statement is the same thing as if-else-if chain. It's not. Switch is the most powerful construct in the C language" - Terry Daves, reworded
@mariovelez578
@mariovelez578 Жыл бұрын
Plus it’s scalable and faster with larger datasets. Switch statements do have their uses, and maps have their separate uses. Know when to use each of them
@filiecs3
@filiecs3 Жыл бұрын
Ah yes, dynamically allocating memory for a map is totally worse than a jump table. What you are looking for is something like a switch expression, but I have no idea if Flutter has that.
@pmberry
@pmberry Жыл бұрын
Nothing stopping you putting the case, statement, and break all on the one line for compactness; with tabbing to align them vertically for readability...
@random6033
@random6033 Жыл бұрын
i mean... in that specific case, it makes sense, but it depends on a situation like when you want to execute more complex instructions, you can obv always have some array of functions and execute the right function or whatever, but it doesn't always make sense to do it
@chrisakaschulbus4903
@chrisakaschulbus4903 Жыл бұрын
Yeah... he just said "don't use switch as a lookup table"... which i'm sure switch isn't meant to be anyway.
@pablolimo8481
@pablolimo8481 Жыл бұрын
It’s attractive to the eye but older devs that see memory leaks as a possible issue this technique isn’t gonna look great, in js the object looks very simple but this wouldn’t be so popular in a hashmap. I’ve seen this endlessly in js projects and when it scales up we end up with constant.js files with dozens of js objects, not sure it’s the most optimal thing either.
@danber1893
@danber1893 2 жыл бұрын
What if we want to call a function? And do something more complicated stuff, instead just print a string. In this case, it doesn't work.
@chrisakaschulbus4903
@chrisakaschulbus4903 Жыл бұрын
"What if we want to call a function?" You're asking too many questions. That's how people disappear.
@perfectionbox
@perfectionbox Жыл бұрын
I benchmarked switch vs. std::map in C++. The map is over two orders of magnitude slower. Although that may not matter if your calls are not in a critical loop.
@notanenglishperson9865
@notanenglishperson9865 Жыл бұрын
TL;DR Use switch case if you have no default value and your type is enum. TS;WM Switch might actually be useful, if you're not using a "default value", because map doesn't track null appearance. Switch case lets you know when you're not covering all of the possible outcomes. Not particularly useful for your example, since your type is a string that can be just about anything, but in case of enums it is ideal.
@gmodrules123456789
@gmodrules123456789 Жыл бұрын
Switch case with strings almost always compiles to a big ‘if else’ block. Switches with strictly numeric values will optimize to an array or to raw jmp statements, which is a lot faster than if else or map.
@kbtankou3155
@kbtankou3155 Жыл бұрын
But ... it works only when you want to return directly a value in the switch, switch are usefull for many other cases
@richiechandra1283
@richiechandra1283 2 жыл бұрын
How about using the enhanced enum instead? I think it will be better and avoid mistyping the key
@Clon1998
@Clon1998 2 жыл бұрын
Sadly they aren’t supported in some language. Dart/Flutter for example added support for that just in the last 6 months
@perfectionbox
@perfectionbox Жыл бұрын
You can also arrange case statements and their code on a single line each, greatly improving readability close to a map.
@sandersaelmans1401
@sandersaelmans1401 Жыл бұрын
Another benefit you’re not taking into account is that switch statements in combination with an enum are exhaustive (as long as you don’t use a default case).
@JOELwindows7
@JOELwindows7 Жыл бұрын
This is only works for functional value returns like shown. Switch case still useful if the switch case is about different ways of execution or algorithm. Also not all programming language have complete feature set of Map like shown, either lacks or not supported at all. But still though. Amazing awesome yey thancc
@adrielschmitz
@adrielschmitz Жыл бұрын
Nice, but I preferer to do some like this: const getCaffeine = (brand: string) => ({ 'Coffee': '95 mg', 'Redbull': '147 mg', 'Tea': '11 mg', 'Soda': '21 mg'})[brand] || 'Not found'
@nicollasoliveira9697
@nicollasoliveira9697 Жыл бұрын
Normally when using enums, I prefer to use switch, due to if someone add a new value to the enum, the switch will warn that it needs a new case
@esbensloth
@esbensloth Жыл бұрын
You should never use strings, they're bad practice; rather you should use morse code stored within a bitmask.
@gabrielsosa3753
@gabrielsosa3753 Жыл бұрын
Dont like this type of "Never do this again" in this particular case you are right but someone could interpret it as "switch case is bad" when it is just a tool. Programmers often have the problem of "when you have a hammer every problem starts looking like a nail". No tool is good or bad but they have different use cases so just learn that they exist and how they work so when you find a problem you can choose which tool is better suited for the job.
@JohnWilliams-gy5yc
@JohnWilliams-gy5yc Жыл бұрын
This will be featured in language conferences. This means it will blow up someday. Please do not flag this as misinformation even though it is. BTW FYI dart documentation is not supposed to be your 1st langauge. If unvoidable, learn some other close-to-metal languages and data structures, so you won't end up making something like this.
@riadhhs4378
@riadhhs4378 Жыл бұрын
Sometimes a case can contain non factorable code or maybe other functions calls
@mahmood466
@mahmood466 Жыл бұрын
For the cases where we just return a value this seems nice, but when there is logic involved, then undoubtedly switch is the to go to solution.
@IgorGuerrero
@IgorGuerrero Жыл бұрын
This is bad code masquerading as good code... careful, these are not equivalent and in just a tiny more complex examples (run functions in the values) would have got jump back to switches since these are not evaluated until called. And the String "caffeine" isn't needed and it's only there to make the hash map example viable.
@msolace580
@msolace580 Жыл бұрын
map requires knowledge of map and its layout, switch is vastly more understandable by more people and is perfectly readable, even if it takes up more lines, and is faster.
@based424
@based424 Жыл бұрын
I feel like that code is cleaner for simple things like returning a string, though if you want more complex functionalities Switch statements still seem to be the way to go, their structure seems much better for more complex functions.
@StarfoxHUN
@StarfoxHUN Жыл бұрын
Why ppls always shows the most bulkiest way when showing a switch? If you just simply return from the switch directly, the first code snippet would look pretty good already whitout sacrificing the flexibility of using switch.
@jaysistar2711
@jaysistar2711 Жыл бұрын
Sure, that is cleaner, but dataflow isn't how switch is meant to be used, anyway; it's for controlflow. Using switch isn't always bad.
@mynameis9817
@mynameis9817 Жыл бұрын
This is really depend on language your using , There's nothing wrong in using switch statements and sometimes you don't even have a choice just to use them, in c++ using switch statements is consider to be good than using if else when dealing in big enumerated value. I rather choose switch statements rathen than using map, or any other storage type.
@lme4339
@lme4339 2 жыл бұрын
Interesting. In my new project, I was thinking about using switch statements instead of super nested if else statements, but maybe this is a much better solution. 🤔🤔🤔🤔
@FlutterMapp
@FlutterMapp 2 жыл бұрын
Let me know if you end up using it 👍💪💪🔥
@Xenonox
@Xenonox 2 жыл бұрын
Keep in mind that this solution has a drawback compared to switch-case statements, especially when used inside a widget tree. Instantiating a map with a considerable amount of data or widgets inside it, will increase the usage of memory and decrease performance.
@Xenonox
@Xenonox 2 жыл бұрын
Also: The example shown in the video could be more readable without removing the switch-case statement. Just return the value directly instead of using the String caffeine and you're down to just 12 lines of Code instead of 20.
@ryanmah4769
@ryanmah4769 Жыл бұрын
Bruh imagine allocating space for a hashmap that'll immediately be deallocated on return because you think switch statements are ugly
@iDontProgramInCpp
@iDontProgramInCpp Жыл бұрын
Just use an enum instead! It's basically an integer, so it's N times more efficient than a string comparison and the difference will show when you have thousands of entries
@JulianColeman03
@JulianColeman03 Жыл бұрын
I mean, sure. Using an object is great. But making the blanket statement that switch-case is bad, is pretty bad. Embedded C programming really benefits from the compiler optimization of using switch-case for smaller footprints.
@a-minus-b-equals-a
@a-minus-b-equals-a Жыл бұрын
Both are bad. Make a Drink type, give it a "Caffeine" property, and call it that way. Alternatively, make an interface and make a class per drink(in case you require complex computations specific to the drink). Then, you can easily expand it with the use of a database, which will make it easy to compute even thousands of cases.
@melonenlord2723
@melonenlord2723 Жыл бұрын
i find this solution less readable xD like the normal if more and there is no need to declare a var caffeine if it only gets returned so i would do: if(type==='Coffee')return '95 mg' if(type==='Redbull')return '147 mg' ... return 'not found' so you always see that type is the thing you are looking for, if the list is long.
@RivaanRanawat
@RivaanRanawat 2 жыл бұрын
This is really helpful! Some languages don't have support for enhanced enums, unlike Dart. This technique can be used there! Thanks for the video Louis!
@FlutterMapp
@FlutterMapp 2 жыл бұрын
Pleasure Rivaan!! Thanks for the support! We need to talk youtube more often! 😂👏 How is going your channel?
@GiovanniDiSanto
@GiovanniDiSanto Жыл бұрын
The switch statement was not invented for this use case, that's why it looks bad if compared to a map or an enum. It mainly used when the logic performed by the program is different depending on the value of the variable the switch is applied to.
@theMagos
@theMagos Жыл бұрын
You could one-line each case with a return and it would be pretty clean, imho.
@ceving865
@ceving865 Жыл бұрын
Object attribute look-ups may be slower than a switch statement.
@sledgex9
@sledgex9 Жыл бұрын
Cleaner yes. How about efficiency/performance?
@suika635
@suika635 Жыл бұрын
If you don't make the map static, it will sort its elements every time the function gets called.
@kucingmalaya1177
@kucingmalaya1177 Жыл бұрын
Am I the only never use switch-case? Use if-else everywhere
@aanshuk
@aanshuk Жыл бұрын
I just clicked on this because it uses switch statements - and I use those a lot. This is actually something I'll be using in my day to day programs!
@gameplaydosabao
@gameplaydosabao Жыл бұрын
But initialize a map object for each getCafeine execution is not good. Switch is build on compilation time and your map on execution time. I agree to write a cleaner code, but not beyond the point to sacrifice performance.
@PyTutorials
@PyTutorials Жыл бұрын
This video completely ignores the fact that switch statements can be used for executing blocks of code and not just mapping values. Yes, the map is better for this case but is definitely not the solution to all switch statement usages.
@user-gf9ri4wj5h
@user-gf9ri4wj5h 2 жыл бұрын
I don’t think switch case is not clear… It’s very clear and readable😑😑😑
@Whynot83848
@Whynot83848 Жыл бұрын
This is not necessarily good advice. Using a map internally hashes the keys. Which is more work. Instead switch case guarantees direct access in most languages. Depends on the use case, but for most part switch case should work.
@safatkhan676
@safatkhan676 Жыл бұрын
Either you don't know what you are talking about or I don't. How can using a map replace the functionality of a switch? A switch case is NOT a data structure. You are treating it as though its purpose is to store data. It is a conditional statement (like if statements) where you can run whatever code you like if a certain case-condition is met.
@myview9923
@myview9923 Жыл бұрын
Now you are allocating memory for the map.
@kedarguruu
@kedarguruu Жыл бұрын
I learned much more from the comments than the video
@notreallyokay9355
@notreallyokay9355 Жыл бұрын
what if the value from a switch statement may come varying functions that require varying parameters, such as deserializing something including its context
@pratikbhagwat950
@pratikbhagwat950 Жыл бұрын
What happens when I have to call a function inside of a switch statement. How do you map a string to function?
@HardikGhoshal
@HardikGhoshal 2 жыл бұрын
C by default implements hash in switch statements good to see y'all catching up
@arcanernz
@arcanernz Жыл бұрын
I didn’t know people use switch statements to returned mapped values.
@victor_silva6142
@victor_silva6142 Жыл бұрын
Thanks that was pretty fun to learn!
@user-cf5uf7vf2g
@user-cf5uf7vf2g 2 жыл бұрын
everyday someone leave from project, someone read others code, no harm to use switch which is common in all language, but not ??
@ibm30rpg
@ibm30rpg Жыл бұрын
If you can't read switch statements by the time you reach data structures then there's something fundamentally off with your programming foundation.
@rothbardfreedom
@rothbardfreedom Жыл бұрын
Or you can just a type Coffee with an abstract function _getCaffenine_. Then you create subtypes.
@FreeDomSy-nk9ue
@FreeDomSy-nk9ue Жыл бұрын
If you need to do some calculations depending on the value, you'd be back where you started with a switch statement. There's nothing bad about switch statements in the example you provided!!!
@steinis6409
@steinis6409 Жыл бұрын
I dont think that its "better to read" that just a matter of taste. The Switch statement therefore has the advantage that you can put addtional / extra lines of code to individual cases because you can place as many lines of code as you want between case XY: and break; but if you get the case that you have to implement an addtional-extra behaviour to a specific case you are rewritig the function to the switch-statement to fix this. :) // In the very-base cases its not hardcoded at all but dynamically set-up by a database and modifyable by options dialogue.
@BoghNorh255
@BoghNorh255 Жыл бұрын
you can do the very same thing with functions, it's quite cool double velocity = 0; Function car(String action) { final map = { 'stop': () { velocity = 0; print('car stopped'); }, 'acelerate': () { velocity += 5; print('car Speeding'); }, 'open door': () { if (velocity > 0) { print('cannot open dor'); return; } print('dor opening'); }, 'change gear': () { if (velocity > 0) { print('cannot change gear while stopped'); return; } print('guear changed'); }, }; return map[action] ?? () { print('cannot do that'); }; } car('acelerate')();
@JNeathawk
@JNeathawk Жыл бұрын
this completely fails once you want to do anything other than simply return a simple lookup value (which anything similar in effect to an associative array will work fine). there's nothing wrong with switch statements, but there is something wrong with telling people a certain coding structure is bad for some arbitrary reason
@magnuswright5572
@magnuswright5572 Жыл бұрын
This is fine for readability and HORRIBLE for performance. I don't know what language you're using, but any compiled language will optimize a switch statement into a jump table, which is way faster than allocating a map on the heap, initializing it, and then binary searching through it to find your value. Your technique also kinda falls apart if you're doing any actual work in the switch statement, because then you have to pull that out into discrete functions (or lambdas) and put references to that in the map, which is probably good practice but also probably doesn't make things much if any easier to read.
@magnuswright5572
@magnuswright5572 Жыл бұрын
I guess the performance hit goes away if the map is only initialized once, rather than every time the function is called, but that's not what you did in this example.
@tarikeld11
@tarikeld11 Жыл бұрын
What do we write if two different types should return the same caffeine value? Do we need one line for each type or can we say something like (type1 || type2) : '10mg'
@nicolasbertozzo2997
@nicolasbertozzo2997 Жыл бұрын
Python3.10: Yeah I should definitely add that
@_____JUNPark_____
@_____JUNPark_____ Жыл бұрын
Python does have switch. But they use 'match' instead of 'switch' And they don't need to use break. (Break is enabled by default) And they also use 'case x' instead of 'default'
@Hughsie28
@Hughsie28 Жыл бұрын
Never watched someone spend 3/4 of a video to repeat the same sentances twice before getting to the point... When you check the performances, "Switch" statements are fastest (unless you are dealing with large ie >100 entries then href maps can work better) (if/elseif/else is slowest) In addition depending on your programming language, compilers will replace your maps/if statements with switchs also if data doesn't change
@Hithori
@Hithori Жыл бұрын
Java has alternative variant like switch (variable) { case 1 -> doSomething(); ... }
@burningglory2373
@burningglory2373 Жыл бұрын
If only I wasnt a simple c programmer
@joaoguilhermezatiribeiro6060
@joaoguilhermezatiribeiro6060 Жыл бұрын
In python I use "dictionary", the same strategy, but nobody teach me, I used by instinct hehe
@dra6o0n
@dra6o0n Жыл бұрын
If you are gonna make a list of choices you are better off making a actual code that is a list, not a list of codes. Make a tuple or array and put your options in there as a condition to trigger while using if else statements elsewhere for the action.
@freyn_dnb
@freyn_dnb 2 жыл бұрын
more vids like that pls, rly good one
@FlutterMapp
@FlutterMapp 2 жыл бұрын
Thank you very much! 💪🔥🔥
@Cestpasfaux-
@Cestpasfaux- 2 жыл бұрын
It's cleaner, but it'll take place in memory. Not that much, but on a big project it can be a problem. As the others say, guys use an enum ^^ Still interesting thought.
@FlutterMapp
@FlutterMapp 2 жыл бұрын
Thanks for the information, I was not aware of the memory issue to be honest. Always cool to learn also 👏🔥🔥
@loknathshankar5423
@loknathshankar5423 Жыл бұрын
Switch case is faster than lookup so think if performance is important to you.
@ABHISHEK-jc8kn
@ABHISHEK-jc8kn 2 жыл бұрын
Nice. I never thought about null or operator that way
@ABHISHEK-jc8kn
@ABHISHEK-jc8kn Жыл бұрын
Ok so i thought about this for a while. Some problems can emerge. The map shouldnt be created for every function call. We can even do this with list.asMap()[i] or list[i].catcherr(null). Also, we could use functions as values and use it for something like a responsive builder. Cool stuff for sure.. definitely refractoring with this and using this for future projects. Hats off
@vedqiibyol
@vedqiibyol Жыл бұрын
I agree, switch statements are pretty ugly, however in C they can provide a slight optimisation, I think this is more useful in embedded, and in some cases using a simple bunch of if-else might just do. Though the switch statment is a good idea, the syntax should simply be revised a little... Also I can't help but notice a slightly french sounding accent! x3
@kokoro5516
@kokoro5516 Жыл бұрын
Someday that Map will be refactored to Switch Case hahaha
@Spartan322
@Spartan322 Жыл бұрын
That's not exactly the case if the language doesn't support automatic fallthrough, in C/C++ the two cases would pretty much compile to the exact same thing but that supports automatic fallthrough, in C# the switch would likely be very slightly more performant and it doesn't perform automatic fallthrough however the break is still required (which is stupid honestly, why eliminate it but keep the requirement for the break?) making it better to use the lookup table in C# in such a simple case. In other languages it depends but if they perform automatic fallthrough or requires breaks anyway it is easier to read the lookup table.
@broomybroomybroomy
@broomybroomybroomy Жыл бұрын
Well yeah, a switch is dumb in this example, since you have 1 input and 1 output. A switch is much more readable and elegant than “if” blocks in many cases.
@victoravr10
@victoravr10 Жыл бұрын
Next: if statement, variables and clases are bad
@nikitafamily5341
@nikitafamily5341 2 жыл бұрын
Thanks a lot! I forgot a map forever in this case 🤣
@kur0sh1
@kur0sh1 Жыл бұрын
Switch is faster than using maps , so if speed is the most important thing id go with switch
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 407 М.
10x Your Speed With Flutter
8:04
Flutter Mapp
Рет қаралды 32 М.
هذه الحلوى قد تقتلني 😱🍬
00:22
Cool Tool SHORTS Arabic
Рет қаралды 94 МЛН
小丑和白天使的比试。#天使 #小丑 #超人不会飞
00:51
超人不会飞
Рет қаралды 36 МЛН
English or Spanish 🤣
00:16
GL Show
Рет қаралды 16 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,7 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Why I Don't Use Else When Programming
10:18
Web Dev Simplified
Рет қаралды 1,2 МЛН
The Most Legendary Programmers Of All Time
11:49
Aaron Jack
Рет қаралды 556 М.
Top 30 Flutter Tips and Tricks
6:50
Flutter Mapp
Рет қаралды 553 М.
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,1 МЛН
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН
Don't Write Comments
5:55
CodeAesthetic
Рет қаралды 797 М.
35 Flutter Tips That Will Change Your Life
10:53
Flutter Mapp
Рет қаралды 302 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,5 МЛН