1

我搜索了一种自动工具来重构和重新排列 c# 代码块以提高控制流的复杂性。我有一个凌乱的 C# 代码,它使用“GOTO”和“switch”而不是“while & for”循环:

        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;
    }

这是非常复杂的自动生成的意大利面条 C# 代码。我尝试通过 Re-sharper & VS 手动改进它,并用“For”循环替换“GOTO”循环并重新排列代码块,我得到了这个代码:

        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;
    }

我想让代码更具可读性和格式。但是手动更改非常困难且耗时。那么是否有任何工具可以分析逻辑并自动执行此过程?

谢谢

4

0 回答 0