i using zmq (zeromq) ipc -
if can message, want handle message, routing desired handler function, either function named one
or two
(or three
).
var messagehandler = { one: function(msg){}, two: function(msg){} }
now, there 2 ways know of doing this:
i put in bunch of case
statements so:
reply.on('message',function(msg){ switch(msg){ case 'one': messagehandler.one(msg); break; case 'two': messagehandler.two(msg); break; default: /// } }
or got more of shorthand approach so:
reply.on('message',function(msg){ if(typeof messagehandler[msg]=='function'){ messagehandler[msg](msg); } else{ throw new error('no handler error'); } }
is there reason use 1 not other?
it seems clear best way second way:
if(typeof messagehandler[msg]=='function'){ messagehandler[msg](msg); } else{ throw new error('no handler error'); }
the reasons being:
- smaller code base
- the program matches input ('msg' var) , output (the matching on non-matching function name) pre-emptively, without having maintain case statements.