Click here to Skip to main content
14,546,341 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: In defense of goto Pin
Member 79891222-Jan-20 23:40
MemberMember 79891222-Jan-20 23:40 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 3:59
mvahoney the codewitch3-Jan-20 3:59 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 4:54
MemberMember 79891223-Jan-20 4:54 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 4:58
mvahoney the codewitch3-Jan-20 4:58 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 5:27
MemberMember 79891223-Jan-20 5:27 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 5:29
mvahoney the codewitch3-Jan-20 5:29 
GeneralRe: In defense of goto Pin
pkfox3-Jan-20 1:24
professionalpkfox3-Jan-20 1:24 
GeneralRe: In defense of goto Pin
Member 79891222-Jan-20 22:43
MemberMember 79891222-Jan-20 22:43 
You should write this as a 2D table of delegates, indexed by firstSym and secondSym. No "else if" tests at all, just two simple multiplications plus an addition (fistSym * secondSym * sizeOfADelegate + baseAddress) to find the delegate to run. At C# level, you obviously see it as plain indexing, not as multiply and add.

Most likely, the table would be quite sparse. If the span of symbol values is large, making the table huge, you may want to use tricks to compress it. A delegate reqires quite a few bytes of storage, so if your table is large, but the number of elseif-bodies, actions, is moderate, your 2D table might contain not delegates, but byte or halfword delegate indexes into a dense delegate array.

Note that if the same delegate action is used many places in the table, with a direct delegate table you would repeat the space overhead many times. With an byte/halfword index table and a separate delegate array, you pay the delegate space overhead only once per action routine; each new reference to it requires only another byte or halfword (when your 2D table is sparse and you have packed it).

If your actions are brief, just a single statement or two, you might avoid all the delegate overhead (both in space for the delegate table and the run time overhead) by setting up the actions as a switch case. If you make the entries in your 2D table not as plain integers but an enum type with symbolic value names that describe the semantics of the actions, it might in fact be quite readable!

I created such a framework this Youle vacation, and was positively surprised by how compact and efficient the driver could be made, how compact the tables turned out, and the advantage of having a single copy of the action code used in several different cases. You fix a bug, or add an extension, in one place; you don't have to duplicate the fix in six different elseifs. The most important thing, though: Any empty square in the 2D table is a red flag: Here is a case you forgot to handle! Check it up!

In my framework, you edit the body of each action by itself, and you edit the 2D table by itself. Then a generator (optionally) packs the table, and creates either a delegate table or a switch statement, and a driver function (which is oblivious to the problem at hand; the only reason for generating it for every model is to strip off the lines not required because no action calls for that specific functionality, such as a third dimension of the table). One other reason to use a generator is that the 2D table is independent of programming language; the action bodies exist in multiple instances in C#, C++, C, Python or whatever language you have written a generator for (which is a fairly trivial matter). The first "language" alternative is a pseudocode alternative that can be used as a model for other languages. You edit the action body only; the generator takes care of creating function headers, adding any (language independent) comment block at the top of the function etc. The red tape of coding complex flows is drastically reduced - once you've got that 2D table in place!

As a convenience feature, I added a red flag alternative: Given an event (in your case: second symbol) in a given state (first symbol), the driver scans the state row of the packed table for events that have a defined action. If you have defined a red flag column to your table, it will be at the end of list, substituting for all the empty table cells that have been compressed away. So I can make a general catch-all error handler: Illegal event (/secondSymbol) xxx in state (/firstSymbol) yyy.

I am considering presenting this framework as a CP article once it is polished. I really would like to make it as a VS plugin, but I have not yet learned how to make VS plugins. (Hints to a good guide would be welcome!) As of now, I must generate the code outside VS (which is fast, but still...), and if you edit the code in VS you must import it back in for the next time you generate it. That is a little cumbersome, so I guess that to make it generally usable, I must provide a VS plugin for it.
GeneralRe: In defense of goto Pin
honey the codewitch2-Jan-20 22:50
mvahoney the codewitch2-Jan-20 22:50 
GeneralRe: In defense of goto Pin
jsc422-Jan-20 23:15
professionaljsc422-Jan-20 23:15 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 5:05
mvahoney the codewitch3-Jan-20 5:05 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 0:14
MemberMember 79891223-Jan-20 0:14 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 3:56
mvahoney the codewitch3-Jan-20 3:56 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 5:16
MemberMember 79891223-Jan-20 5:16 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 5:23
mvahoney the codewitch3-Jan-20 5:23 
GeneralRe: In defense of goto Pin
glennPattonWork2-Jan-20 23:13
professionalglennPattonWork2-Jan-20 23:13 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 0:35
MemberMember 79891223-Jan-20 0:35 
GeneralRe: In defense of goto Pin
glennPattonWork3-Jan-20 0:41
professionalglennPattonWork3-Jan-20 0:41 
GeneralRe: In defense of goto Pin
Member 79891223-Jan-20 1:32
MemberMember 79891223-Jan-20 1:32 
GeneralRe: In defense of goto Pin
Rick York3-Jan-20 5:02
mveRick York3-Jan-20 5:02 
GeneralRe: In defense of goto Pin
Mark_Wallace2-Jan-20 23:48
MemberMark_Wallace2-Jan-20 23:48 
GeneralRe: In defense of goto Pin
honey the codewitch3-Jan-20 4:01
mvahoney the codewitch3-Jan-20 4:01 
GeneralRe: In defense of goto Pin
Mark_Wallace3-Jan-20 19:58
MemberMark_Wallace3-Jan-20 19:58 
GeneralRe: In defense of goto Pin
Greg Utas3-Jan-20 1:44
professionalGreg Utas3-Jan-20 1:44 
GeneralRe: In defense of goto Pin
H.Brydon4-Jan-20 16:21
professionalH.Brydon4-Jan-20 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.