1

我在 VS2013 RC 中选择“Analyze > Analyze Solution for Code Clones”,期望它能够实现这两种方法:

private static char GetBarcodeChecksumWithLegacyCode(string barcodeWithoutCzechSum)
{
    Contract.Requires(!string.IsNullOrWhiteSpace(barcodeWithoutCzechSum));

    if (barcodeWithoutCzechSum.Length > 6)
    {
        int a = 0;
        int b = 0;
        int j = barcodeWithoutCzechSum.Length - 1;
        int i = j;
        while (i >= 0)
        {
            a = a + barcodeWithoutCzechSum[i] - 48;
            i = i - 2;
        }

        j = barcodeWithoutCzechSum.Length - 2;
        i = j;
        while (i >= 0)
        {
            b = b + barcodeWithoutCzechSum[i] - 48;
            i = i - 2;
        }
        a = 3 * a + b;
        b = a % 10;
        if (b != 0) b = 10 - b;

        var ch = (char)(48 + b);
        return ch;
    }
    return ' ';
}

public static string GetBarcodeChecksum(string barcode)
{
    int oddTotal;
    int oddTotalTripled;
    int evenTotal;
    // Which positions are odd or even depend on the length of the barcode, 
    // or more specifically, whether its length is odd or even, so:
    if (isStringOfEvenLen(barcode))
    {
        oddTotal = sumInsideOrdinals(barcode);
        oddTotalTripled = oddTotal * 3;
        evenTotal = sumOutsideOrdinals(barcode);
    }
    else
    {
        oddTotal = sumOutsideOrdinals(barcode);
        oddTotalTripled = oddTotal * 3;
        evenTotal = sumInsideOrdinals(barcode);
    }
    int finalTotal = oddTotalTripled + evenTotal;
    int modVal = finalTotal % 10;
    int czechSum = 10 - modVal;
    if (czechSum == 10)
    {
        return "0";
    }
    return czechSum.ToString();
}

...在功能上是等效的。第一个是神秘的,第二个(对我来说,也许是因为它是我写的)是简单的。不过,该工具并不认为它们是“代码克隆”。

是不是因为我的方法调用了其他方法,即isStringOfEvenLen()、sumInsideOrdinals()、sumOutsideOrdinals()?

4

0 回答 0