Super Combo: Map + Function Pointer
Anyone who has coded with me, if asked to describe me in three words would say…
Function-Pointer, Enumeration, Map
When I get to combine 2 or more of these things together, I am usually pretty excited! Map + Function Pointer to me is like a kick in the face to code duplication!
Why is this little combo so awesome?
Let me give you a little code example:
Rewriting this as a super-awesome-hard-to-contain-myself map of function pointers, you get:
Now if we forgo the use of strings, but use an enumeration from the get go… We achieve the most-super-awesome-ultimate-combo MAP OF ENUMERATIONS TO FUNCTION POINTERS!!!
Why is this so super awesome?
It isn't as apparent in a little code snippet, but imagine an application where you have many sections of code that deal with this logic in the same way. Often you encounter a third-party API that has methods or properties that are named after the type of data they work on or retrieve instead of having a parameter passed in. In situations like these, it is very useful to map the mapping between data and logic into one location.
I also don't like switch statements very much. They can be confusing with the break statements in there, and end up being duplicated all over of the code base. Anytime you see a switch statement in code, it is a possible candidate for the super-fantastic-mega-awesome-30-hit-combo of Map + Function pointer.