RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:34 pm
(This post was last modified: August 17, 2022 at 4:31 pm by bennyboy.)
(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.Does it say why?
Let me ask you a question. In your .js file (Hi, mom, I got mentioned in someone's github!), you have a series of conditions formed like this:
Code:
if ((currentDirective & 0xff000) === 0x00000) { // Do your LOAD register,register stuff }
else if ((currentDirective & 0xff000) === 0x01000) { // Do your LOAD register,constant stuff }
So you will have to do that bit operation every time. Why do you not do this:
Code:
var filteredDirective = currentDirective & 0xff000;
if (filteredDirective === 0x00000) { // Do your LOAD register,register stuff }
else if (filteredDirective === 0x01000) { // Do your LOAD register,constant stuff }
Now, personally, I don't think that this difference will matter much unless you have hamsters running the code, but it seems like a lot of unnecessary operations to me.
Also, for style, I'd probably replace the hex constanst with meaningful labels, so you can check them all in code easily without scanning through many nested conditionals, though I don't know enough about JavaScript to know if it is comparable with "===":
Code:
const LRR = 0x00000; // Load register, register
. . .
if (filteredDirective === LRR) { // Do your LRR stuff }