Restrict number of control structure depth for acceptable code(Too much if else condition)

  • Thread starter Thread starter La07K
  • Start date Start date
L

La07K

Guest
Hello,

I have a function its actually checking some errode code and filling it into a class property field.But the function contanisn almost 5o if else statement

if(aaa)

{

}

else if(qqqq)

{

}

else if(ppp)

{

}

........etc 50 times.How can i reduce the control depth of this function.Please c the code snippet below



public class HWResult
{

public HWError HwError { get; set; }

public ErrorCode errorCode { get; set; }





public HWResult(SystemResult r)
{
ErrorCodeMappToSpecifErrorValue(r);
}

private void ErrorCodeMappToSpecifErrorValue(SystemResult r)
{
HwError = new HwError(r.HwError);
if (HwError.X1)
{
errorCode = ErrorCode.X1;
}
else if (HwError.X2)
{
errorCode = ErrorCode.X2;
}
else if (HwError.X3)
{
errorCode = ErrorCode.X3;
}
else if (HwError.X4)
{
errorCode = ErrorCode.X4;
}
else if (HwError.X5)
{
errorCode = ErrorCode.X5;
}
else if (HwError.X6)
{
errorCode = ErrorCode.X6;
}
else if (HwError.X7)
{
errorCode = ErrorCode.X7;
}
else if (HwError.X8)
{
errorCode = ErrorCode.X8;
}
else if (HwError.X9)
{
errorCode = ErrorCode.X9;
}
else if (HwError.X10)
{
errorCode = ErrorCode.X10;
}
else if (HwError.X11)
{
errorCode = ErrorCode.X11;
}
else if (HwError.X12)
{
errorCode = ErrorCode.X12;
}

.................etc
else if (HwError.X50)
{
errorCode = ErrorCode.X50;
}
}


public HWResult(uint value)
{
ErrorCodeMap(new SystemResult(value));
}
}

public class HWError
{

public ushort ErrorType { get; set; }


public uint ErrorCode { get; set; }
public bool X1
{
get { return ((ErrorType == ErrorType.X1) && (ErrorCode == HWErrorCode.X1)); }
}
public bool X2
{
get { return ((ErrorType == ErrorType.X2) && (ErrorCode == HWErrorCode.X2)); }
}
......................................etc

public bool X50
{
get { return ((ErrorType == ErrorType.X50) && (ErrorCode == HWErrorCode.X50)); }
}
}

public enum ErrorType
{

X1,
X2,
X3,......etc,
X50

}

Can anyone suggest how can i optimize this method efficiently.This piece of code looks ugly.

Plz help me to address this problem.







Coding.....................................

Continue reading...
 
Back
Top