mardi 22 septembre 2015

C++ code refactoring for if scenario

I'm looking to refactor the c++ code for if conditions which will reduce the number of lines of code as well as it should have min complexity.

Here is the example:

if (xyz->a != cmd->aa)
{
   xyz->a = cmd->aa;
   obj->isFound = true;  //common code for all ifs
}


if (xyz->b != cmd->bb)
{
    xyz->b = cmd->bb;
    obj->isFound = true; 
}

And so on.. Here a, b, aa, bb are defined as a struct element.

Another example having if condition with arrays:

 if (abc->r16[0] != cmd->r0m)
    {
        abc>r16[0] = cmd->r0m;
        obj->isFound = true; //some common code for all ifs
    }


if (abc->r16[1] != cmd->r1m)
        {
            abc>r16[1] = cmd->r1m;
            obj->isFound = true; //some common code for all ifs
        }

And so on for r16[0] to r16[15]. Here r16[15] is defined inside struct.

Last scenario is for if condition with multidimensional array:

     if (pqr->c_0_15_r_0_15[0][0] != cmd->obj0000)
           {
              pqr->c_0_15_r_0_15[0][0] = cmd->obj0000
              obj->isFound = true; //some common code for all ifs
           }

if (pqr->c_0_15_r_0_15[1][0] != cmd->obj0100)
           {
              pqr->c_0_15_r_0_15[1][0] = cmd->obj0100
              obj->isFound = true; //some common code for all ifs
           }

if (pqr->c_0_15_r_0_15[2][0] != cmd->obj0000)
           {
              pqr->c_0_15_r_0_15[2][0] = cmd->obj0200
              obj->isFound = true; //some common code for all ifs
           }

Here c_0_15_r_0_15[2][0] will go through [0][0] to [15][0] and then [0][1] to [15][1] and so on...

For all such if condition scenario there will me 100's of if statements which should be reduced. How can we refactor such code?

Aucun commentaire:

Enregistrer un commentaire