Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to avoid a thousand if statements?

I basically have this problem: right now, we have a system where it gets a string as input, and it basically says ACTION:.

For each of the actions there is an automatically generated function(Rational Rose GRRR), such as

bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());
bouncer_comm.askforname().sendAt(msg->sapindex0());

bouncer_comm returns an RTOutSignal, I can't create them manually because of the bizarre structure rose uses.

Right now, my only option is to create a hundred or so if statements, where I do:

if(action == "CHAT")  bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());

Which is realllllyy annoying.

What would be the best way to avoid this? I've looked at / tried countless things, this is a really old version of rational rose (pre 2k) and yeah.

If anyone has any ideas that would be amazing.

like image 806
UberJumper Avatar asked Nov 30 '22 20:11

UberJumper


2 Answers

I like @cobbal's idea of the function pointer hash above, but you could replace this conditional logic with polymorphism.

see: http://c2.com/cgi/wiki?ReplaceConditionalWithPolymorphism

like image 177
killingmichael Avatar answered Dec 02 '22 08:12

killingmichael


A hash storing function pointers could work well here

like image 36
cobbal Avatar answered Dec 02 '22 09:12

cobbal