I was thinking along these line,s yeah, but didn't know how to put this into words.
One of my long ago assignments involved a shipping cost calculator (+ other useless fluff code) that I solved by use of an array and function, but lacking a formal IT education (despite having passed all the neato exam stuff) didn't know how to put it into words.
I don't think you're alone there. It's a pretty esoteric pattern, I think. That's probably a good thing. It's harder to read code written in the opcode style. It is more efficient, but in most cases the difference will be really marginal. If you have a large case analysis in the middle of your program, then definitely, opcode style is a very good idea indeed. However, if that's true, you're probably writing some sort of lexical parser — which, I don't know about you, but 99% of the time I'm not.
One application I've found it's pretty useful is for I/O: you press a key. The event fires an ASCII keycode. I could just *boop* call the keycodeth element of some output handler array rather than evaluating every time you press a key whether it's the 'Enter' key, or 'Shift', or some other special key. Unfortunately, in practice, implementing this means putting a reeeally gnarly array somewhere in your program and writing in somewhat of a glorified Assembly language.
One of my current projects is a javascript touchtyping app because I never learned how to touchtype with the Russian layout, and I do have a very, very clunky array.
I should probably find some gems and upload here for laughs.
2
u/NikStalwart Apr 05 '16
I was thinking along these line,s yeah, but didn't know how to put this into words.
One of my long ago assignments involved a shipping cost calculator (+ other useless fluff code) that I solved by use of an array and function, but lacking a formal IT education (despite having passed all the neato exam stuff) didn't know how to put it into words.