1

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

Jake Terro
  • 11
  • 2
  • 1
    This code looks like poorly reverse engineered code... – Reed Copsey Oct 11 '12 at 17:28
  • This is your friend: Shortcut Command: Ctrl + K + D Useful to indent your code to make it more readable. – Furqan Safdar Oct 11 '12 at 17:28
  • It won't and as far as my knowledge is concerned, there isn't any tool available in the market to change the code flow especially replacing GOTO thing – Furqan Safdar Oct 11 '12 at 17:32
  • There are tools which will identify these kinds of problems, like StyleCop, but I'm not aware of any that will fix it for you. What's generating the spaghetti code? Perhaps there's a way to fix the behavior of that tool. – JamieSee Oct 11 '12 at 19:34
  • the spaghetti code was generated by .Net Reflector 7 – Jake Terro Oct 11 '12 at 19:55

0 Answers0