私は以下の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 }
}
}
}
}