健全性检查:将代码提取到功能中断程序中

时间:2019-02-17 00:34:39

标签: swift sanity-check

我有以下for循环:

func test(_ fourHundredYears: [CountPatternPair]) {

    // Iterate over `CountPatternPairs` containing hundred-year `CountPatternPairs`
    for hundredYearCPP in fourHundredYears {

        // Do the below as many times as indicated by the count in the `CountPatternPair`
        for _ in 1 ... hundredYearCPP.0 {

            // Iterate over `CountPatternPairs` containing four-year `CountPatternPairs`
            for fourYearCPP in (hundredYearCPP.1 as! [CountPatternPair]) {

                // Do the below as many times as indicated by the count in the `CountPatternPair`
                for _ in 1 ... fourYearCPP.0 {

                    // Iterate over `CountPatternPairs` containing year `CountPatternPairs
                    for yearCPP in (fourYearCPP.1 as! [CountPatternPair]) {

                        // Do the below as many times as indicated by the count in the `CountPatternPair`
                        for _ in 1 ... yearCPP.0 {

                            // Iterate over each month in the array element of the `CountPatternPair`
                            for month in (yearCPP.1 as! [[DayTotalPair]]) {

                                // Iterate over each `DayTotalPair` in each month
                                for dtp in month {

                                    oeod.append(dtp.1, over: dtp.0)

                                    i += dtp.0

                                    if minLimit.equals(oeod.at(oeod.count - 1)) { return }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我写这本怪兽来测试某些逻辑,目的是当逻辑正常工作时,我会将其重构为递归函数以使其更加简洁。我做到了,但是递归版本不起作用。弄清楚我错过了什么,但最终我将其搁置了,因为对于我的一生,我看不到它是什么。仍在测试中,我决定将一些循环逻辑提取到自己的函数中,以使其更加简洁。不添加任何新逻辑。从我看来,并没有改变任何逻辑的执行顺序。

但令我沮丧的是,重构为单个功能的相同功能也无法正常工作。直接复制并粘贴它,仍然不起作用。尝试在extract to中使用Xcode函数,以防万一是我的复制和粘贴破坏了它,也无法正常工作。

有人可以看看上面代码的以下提取版本,以查看是否有任何事情导致其执行的逻辑不同于上面的代码?

func iterateFourHundredYears(_ fourHundredYears: [CountPatternPair]) {

    // Iterate over `CountPatternPairs` containing hundred-year `CountPatternPairs`
    for hundredYearCPP in fourHundredYears {

        iterateHundredYearCPP(hundredYearCPP)
    }
}

func iterateHundredYearCPP(_ hundredYearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... hundredYearCPP.0 {

        // Iterate over `CountPatternPairs` containing four-year `CountPatternPairs`
        for fourYearCPP in (hundredYearCPP.1 as! [CountPatternPair]) {

            iterateFourYearCPP(fourYearCPP)
        }
    }
}

func iterateFourYearCPP(_ fourYearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... fourYearCPP.0 {

        // Iterate over `CountPatternPairs` containing year `CountPatternPairs
        for yearCPP in (fourYearCPP.1 as! [CountPatternPair]) {

            iterateYearCPP(yearCPP)
        }
    }
}

func iterateYearCPP(_ yearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... yearCPP.0 {

        // Iterate over each month in the array element of the `CountPatternPair`
        for month in (yearCPP.1 as! [[DayTotalPair]]) {

            // Iterate over each `DayTotalPair` in each month
            for dtp in month {

                oeod.append(dtp.1, over: dtp.0)

                i += dtp.0

                if minLimit.equals(oeod.at(oeod.count - 1)) { return }
            }
        }
    }
}

0 个答案:

没有答案