내 코드에서 중첩 된 if-else 조건을 많이 수행합니다. 34 번이나 해. 있잖아, 정말 미치겠 어. 그것을 줄이는 방법을 가르쳐주세요. 고맙습니다.중첩 된 if-else 조건을 줄이는 방법은 무엇입니까?
여기에 내 코드 :
if ($input_age > $age_min && $input_age <$age_max){
if ($input_heigh > $heigh_min && $input_heigh < $heigh_max){
if ($input_ds == "yes" && $ds_val=="yes"){
//some calculation
}
else if ($input_ds == "no" && $ds_val=="yes"){
//some calculation
}
else if ($input_ds == "yes" && $ds_val=="no"){
//some calculation
}
else if ($input_ds == "no" && $ds_val=="no"){
//some calculation
}
}
else if ($input_heigh < $heigh_min || $input_heigh > $heigh_max){
if ($input_ds == "yes" && $ds_val=="yes"){
//some calculation
}
else if ($input_ds == "no" && $ds_val=="yes"){
//some calculation
}
else if ($input_ds == "yes" && $ds_val=="no"){
//some calculation
}
else if ($input_ds == "no" && $ds_val=="no"){
//some calculation
}
}
많은 나 같은 경우에 스위치를 사용하는 것이 좋습니다 경우 - 다른 실행 조건
중첩 된 레이아웃이 마음에 들지 않습니까? 각 계산을 처리하는 함수로 코드를 리팩터링하여 코드를 읽기 쉽게 만들 수 있습니다. – gunnx
@gunnx - 내 마음을 읽었습니까? 나는 비슷한 것을 쓰려고했다. –