PL/SQL Best Practices [10][If Statements]-Flatten Nested IFs
Avoid Unnecessary Nested IFs
The following statements are equivalent. The flat structure expresses the logic more clearly and with less code.
Nested:
1
IF <condition1>
2
THEN
3
4
ELSE
5
IF <condition2>
6
THEN
7
8
ELSE
9
IF <condition3>
10
THEN
11
12
ELSE
13
IF <condition4>
14
THEN
15

16
END IF;
17
END IF;
18
END IF;
19
END IF;
20
IF <condition1> 2
THEN 3
4
ELSE 5
IF <condition2> 6
THEN 7
8
ELSE 9
IF <condition3>10
THEN 11
12
ELSE 13
IF <condition4> 14
THEN15

16
END IF;17
END IF;18
END IF;19
END IF;20

Flat:
1
IF <condition1>
2
THEN
3

4
ELSIF <condition2>
5
THEN
6

7
ELSIF <condition3>
8
THEN
9

10
ELSIF <condition4>
11
THEN
12

13
END IF;
IF <condition1>2
THEN3

4
ELSIF <condition2> 5
THEN6

7
ELSIF <condition3>8
THEN9

10
ELSIF <condition4>11
THEN12

13
END IF;
浙公网安备 33010602011771号