lundi 14 décembre 2015

How to avoid messy nested if-else or switch

Function AM()
{
  var source = new Array("External","Chassis","Internal");
  var shape = new Array("Sine","Square", "Ramp","Nramp","Triangle","ARB");
  var depth = new Array ("100","0","50");
  var frequency = new Array("100","0.002","20000","1000");

  var currentSource;
  var currentShape;
  var currentDepth;
  var currentFrequency;

  for (var item in source)
  {
    currentSource = source[item];
    FG_AMSource().Keys(source[item] );

    for (var item in shape)
    {
      currentShape = shape[item];
      FG_AMShape().Keys(shape[item] );

      for (var item in depth)
      {
        currentDepth = depth[item];
        FG_AMDepth().Keys(depth[item]);

        for (var item in frequency)
        {
          currentFrequency = item;
          FG_AM_Frequency().Keys(frequency[item]);
          CheckImage2(currentSource, currentShape,currentDepth, currentFrequency);
        }// shape
      }// depth
    }// shape
  }//source
}

FG_AMSource() is a function that allows me to set the setting. With what I have here, I am able to loop through all combinations of Source, Shape, Depth and Frequency. My problem here lies in the CheckImage2 function. With each combination I get, I must call this CheckImage2 function, pass in the current Source, Shape, Depth and Frequency, then do checking accordingly. Therefore, inside my CheckImage2 function, it will look something like

Function CheckImage2(currentSource, currentShape, currentDepth, currentFrequency)
{
    switch (currentSource)
        case "External":
            switch (currentShape)
                case "Sine":
                    switch (currentDepth)
                        case "100":
                            switch (currentFrequency)
                                case "100": //External+Sine+100+100
                                case "0.002": //External+Sine+100+0.002
                                //etc etc etc you get the idea
                                //and I need to include all possible combinations
}

What should I do instead?

Aucun commentaire:

Enregistrer un commentaire