RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:57 pm
(This post was last modified: August 17, 2022 at 3:58 pm by HappySkeptic.)
(August 17, 2022 at 3:30 pm)FlatAssembler Wrote: I am a bit amazed people here are recommending me to use switch-case. In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were taught that using switch-case is a sign of bad style. Domagoj Kusalić in his book "Napredno Programiranje i Algoritmi u C-u i C++-u" also says switch-case should be used only rarely, if ever.
I don't know those books, but if I search for "are switch statements bad", I get a bunch of reasons which would be "even more bad" in a bunch of nested if's.
Do you know what one "preferred" solution to a large switch statement is? A map that calls different pieces of code. The map can then be dynamic, and modified at runtime, while a switch statement needs direct modification when new functionality is added.
But, when switching on a fixed set of enumerated types, a switch is preferred. In some languages, switching on an enum will actually give a warning or error if all the values of the enum aren't included as cases (without a default case). In this way, adding values to the enumeration would not be a maintenance nightmare. You get none of that with nested If's.