-1
이것이 원래의 방법입니다. 제 의도는 코드의 세부 사항을 추상화하여 가독성을 높이고 동일한 작업을 수행하지만 더 읽기 쉬운 이름으로 기능을 배치하는 것입니다.왜 이와 동등한 함수가 다른 결과를 생성합니까?
이 첫 번째 방법을 통해 원하는 동작을 얻고 rows [x]가 lineRows에 제대로 추가되었습니다.
def getAllRowsForLine(rows, index) {
def lineRows = [rows[index]]
def newOperatorNotFound
def x = index + 1
if (x <= rows.size() - 1) {
newOperatorNotFound = true
while (x <= (rows.size() - 1) && newOperatorNotFound) {
if (rows[x].PGM_PROC_OPE.trim() == "") {
lineRows << rows[x]
} else if (rows[x].PGM_PROC_TY == "AN" || rows[x].PGM_PROC_TY == "OR") {
lineRows << rows[x]
}
else {
newOperatorNotFound = false
}
x++
}
}
return lineRows
}
여기에 문맥에 대한 상대적인 방법과 함께 리팩터링 된 코드가 있습니다.
이 메서드는 첫 번째 행 [x]이 lineRows에 추가 된 후 루프를 해제하여 원하는 동작을 발생시키지 않습니다.
def getAllRowsForLine2(rows, index) {
def lineRows = [rows[index]]
def newOperatorNotFound
def i = index + 1
if (moreRows(rows, i)) {
newOperatorNotFound = true
while (moreRows(rows, i) && newOperatorNotFound) {
if (operatorEmpty(rows, index)) {
lineRows << rows[i]
}
else if (procTypeAnd(rows, i) || procTypeOr(rows, i)) {
lineRows << rows[i]
} else {
newOperatorNotFound = false
}
i++
}
}
return lineRows
}
def operatorEmpty(rows, index) {
return rows[index].PGM_PROC_OPE.trim() == ""
}
def procTypeAnd(rows, index) {
return rows[index].PGM_PROC_TY == "AN"
}
def procTypeOr(rows, index) {
return rows[index].PGM_PROC_TY == "OR"
}
def moreRows(rows, index) {
return index <= (rows.size() - 1)
}
내가 알 수있는 한이 모든 것들이 동일합니다. 함수의 동등성 테스트를 시도하기 위해 아래의 코드를 실행했다.
println lineProcessor.getAllRowsForLine(rows, 0) == lineProcessor.getAllRowsForLine2(rows, 0)
=> true
질문을 삭제할 수 있습니까? –