mardi 12 juillet 2016

Good way of calling Functions/class in big if/else constructs

I've seen many code-parts in mostly old(java mostly) projects that look like

if(type == typeOne){
    callFunctionOne();
}else if (type == typeTwo){
   callFunctionTwo();
}else if (type == typeThree){
   callFunctionThree();
}//i've seen over ~800 lines like this!

where "type" could be an enum or really anything and the whole thing could be written in switch/case style too. My question is: is there a "better"(more stylish/shorter/more readable) way to achieve this? I've seen constructs in PHP like:

//where $type = "one","two" etc.
$functionName = 'callFunction' . $type; 
new $functionName();

But im not sure if this is realy the "better" way and if its even possible in other Languages.

Aucun commentaire:

Enregistrer un commentaire