I see a lot of people saying that it is wrong to make a switch in the code because it ends up weighing and it is ugly
I do not see this. It may be that I use good programmers as a reference:)
switch
is elegant and fast, so every modern language, like the old ones, has it. Knowing how to use it is very powerful and facilitates a lot, besides being faster than other options in most scenarios. In fact it exists for optimization of execution in the first place, and elegance in the second, so I can not imagine where these ideas come from.
Well, I even imagine it in another scenario. There are some people who say that because they are less object oriented. But if you ask why it's bad, they generally do not know how to say, they read somewhere and they keep repeating. They do not give context, as it happens with those who have incomplete understanding of things (something you are seeking to get rid of here). Of course in some context it is not so suitable to use it, but this goes for all the features of the programming language, without exception, is valid for variables, literals, operators, all commands, even a simple semicolon. >
If the comparison is with if
I would say that it is usually more elegant in cases where it can be used (not all it can). The case shown in the question is pretty basic and the most ideal for your adoption. It gets faster and more beautiful, although pretty is something subjective (it would be more if you did not have to use break
).
Of course, considering the first line, it does not even make sense because it is guaranteed that only the first line will run.
You have a question that talks more about how it works: How does the switch work under the covers? ( although it is another language, works the same)
It may also be useful to read:
I did not even mention its use as pattern matching which is much more powerful and interesting, and is available in C #, although many do not know it. And version 8 can be used as expression .