如何避免凌乱的嵌套 if-else 或 switch
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()
是一个允许我设置设置的函数。有了我这里的东西,我就可以遍历源、形状、深度和频率的所有组合。
我的问题在于 CheckImage2
函数。对于我得到的每个组合,我必须调用这个 CheckImage2
函数,传入当前的源、形状、深度和频率,然后进行相应的检查。因此,在我的 CheckImage2
函数中,它看起来像
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
}
我应该怎么做?
有几种方法可以解决它:
1) 如果操作可以公式化,那么您应该创建公式而不是创建嵌套的for
循环或switch
。像
Function CheckImage2(currentSource, currentShape, currentDepth, currentFrequency)
{
//example, suppose it can be formulated as a string
string formulatedString = currentSource + currentShape + currentDepth + currentFrequency;
//do something else, because things can be formulated instead of listed
}
这是因为您实际需要的是一个 handler/function,它采用任何组合。
2) 如果要填充的组合数量不多,请尝试使用 HashMap
或 Map
或任何等价物,并 预填充可能的组合 这样当你使用时你只需要调用 hashMap[key]
并且最多 1 for
循环然后相应地执行而不是嵌套循环
3) 只要有可能,您也可以将它们分成 更小的独立函数 的片段,它们一次只依赖于一个(或至少更少)元素(而不是所有其中的一个)。
4) 考虑创建一个合适的 class 和 使用 Tree
结构 来解决它
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()
是一个允许我设置设置的函数。有了我这里的东西,我就可以遍历源、形状、深度和频率的所有组合。
我的问题在于 CheckImage2
函数。对于我得到的每个组合,我必须调用这个 CheckImage2
函数,传入当前的源、形状、深度和频率,然后进行相应的检查。因此,在我的 CheckImage2
函数中,它看起来像
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
}
我应该怎么做?
有几种方法可以解决它:
1) 如果操作可以公式化,那么您应该创建公式而不是创建嵌套的for
循环或switch
。像
Function CheckImage2(currentSource, currentShape, currentDepth, currentFrequency)
{
//example, suppose it can be formulated as a string
string formulatedString = currentSource + currentShape + currentDepth + currentFrequency;
//do something else, because things can be formulated instead of listed
}
这是因为您实际需要的是一个 handler/function,它采用任何组合。
2) 如果要填充的组合数量不多,请尝试使用 HashMap
或 Map
或任何等价物,并 预填充可能的组合 这样当你使用时你只需要调用 hashMap[key]
并且最多 1 for
循环然后相应地执行而不是嵌套循环
3) 只要有可能,您也可以将它们分成 更小的独立函数 的片段,它们一次只依赖于一个(或至少更少)元素(而不是所有其中的一个)。
4) 考虑创建一个合适的 class 和 使用 Tree
结构 来解决它