I search about an automatic tool to refactor & rearrange c# code blocks to improve control flow complexity. I have a messy C# code which uses "GOTO" and "switch" instead of "while & for" loops:
public double GetMaximumRange(long startPoint, long endPoint)
{
double range;
double variable1 = _priceArray[(int) startPoint];
double variable2 = _priceArray[(int) startPoint];
var variable3 = (int) endPoint;
var index = (int) (startPoint + 1L);
Label_002F:
if (index > variable3)
{
goto Label_0083;
}
if (_priceArray[index] <= variable1)
{
goto Label_0059;
}
variable1 = _priceArray[index];
goto Label_0078;
Label_0059:
if (_priceArray[index] >= variable2)
{
goto Label_0078;
}
variable2 = _priceArray[index];
Label_0078:
index++;
goto Label_002F;
Label_0083:
range = variable1 - variable2;
return range;
}
This is very complex auto-Generated spaghetti C# code. I tried to improve it manually by Re-sharper & VS and replace "GOTO" loops with "For" loops and rearranged the code blocks, and I got this code:
public double GetMaximumRange(long startPtr, long endPtr)
{
double maximum = _priceArray[startPtr];
double minimum = _priceArray[startPtr];
for (long i = (startPtr + 1L); i <= endPtr; i++)
{
if((_priceArray[i] <= maximum))
{
if (_priceArray[i] < minimum)
{
minimum = _priceArray[i];
}
}
else
{
maximum = _priceArray[i];
}
}
double range = maximum - minimum;
return range;
}
I want to make the code more readable and well-formatted. but manual changing is very hard and time consuming. so Is there any tools that can analyze the logic and make this process automatically?
thanks