News for hackers. Stuff that matters.
Re:The death of if/else cascades, a lightweight alternative by nostrademons on Sunday December 06, 2009 @12:36PM (#979855)
Dispatch tables give you some additional flexibility. For example, say that you want to wrap each function with some statistic-gathering in debug mode. You could implement this easily with:

  if(DEBUG) {
    for(var key in actions) {
      actions[key] = wrapDebug(key, actions[key]);
    }
  }
In a switch or nested if-else, you would need to include that debug processing inline with each alternative, instead of factoring it out into its own closure-generating function.

Similarly, compilers often use this approach for instruction generation. The set of available machine instructions is stored as a table; if you move to a different architecture, you just replace the table with one appropriate to the new architecture.

This is not a new idea. SICP has a whole section on data-directed programming, and MapReduce and many other internal bits of Google infrastructure use this extensively.

Re:The death of if/else cascades, a lightweight alternative by DrJokepu on Sunday December 06, 2009 @12:59PM (#979881)
Compilers are special because their input/output data is code and instructions. However the code of the compiler itself is clearly separated from the code the compiler emits. When the compiler uses the lookup table to look up the code to emit it really looks up data for output and not code to be executed (right away).
Re:The death of if/else cascades, a lightweight alternative by jfoutz on Monday December 07, 2009 @12:55PM (#981621)
Another cool trick is runtime rebinding.

Context sensitive actions come to mind. s means save when editing, or s means stop when running. This is also a common way to let users remap keys, or build their own menus.