vendredi 8 février 2019

Refactoring if-elseif block to hash and classes

I'm working on the legacy asp.net code (however it was converted to newest .net version), I encountered file which contains 15 500 long if-elseif block (with 700 conditions). Each condition is of the same structure:

if(a == "aaa")
{
  .....
}
else if(a == "bbb")
{
 ....
}
.....

I think it needs to be refactored. I want to split the code from each block into separe classes, name them as "aaa", "bbb", ... and use Dictionary or Hashtable to store references to them. It would probably have an Interface with method execute() with the certain return type. The problem is that I think instantiation of each class and storing the objects to hash table would take some time and I believe that performancewise it would be worse thing (for each request constructing hashtable). It would have to store total of 700 objects and then it would retrieve only one object. My questions are: 1. Is there any better way of solving such a problem? For example, maybe I shall store classes instead of objects, and then use reflection to instantiate class and call its method? 2. How painful it would be performance wise to apply such design (dividing if/else if blocks into classes -> add objects (which implement certain interface) to dictionary --> take one object from dictionary 3. Let's say I put all the classes to folder F, how could I retrieve all the classes and add them to dictionary 4. Any suggestions, other ways to solve the problem?

Aucun commentaire:

Enregistrer un commentaire