Skip to content

Commit bc849ca

Browse files
committed
Fix #31 - Created rule G-4250: Avoid using identical conditions in different branches of the same IF or CASE statement.
1 parent e414069 commit bc849ca

File tree

1 file changed

+46
-0
lines changed
  • docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# G-4250: Avoid using identical conditions in different branches of the same IF or CASE statement.
2+
3+
!!! warning "Major"
4+
Maintainability, Reliability, Testability
5+
6+
## Reason
7+
8+
Conditions are evaluated top to bottom in branches of a `case` statement or chain of `if`/`elsif` statements. The first condition to evaluate as true leads to that branch being executed, the rest will never execute. Having an identical duplicated condition in another branch will never be reached and will be dead code.
9+
10+
## Example (bad)
11+
12+
``` sql
13+
declare
14+
l_color types_up.color_code_type;
15+
begin
16+
case l_color
17+
when constants_up.co_red then
18+
my_package.do_red();
19+
when constants_up.co_blue then
20+
my_package.do_blue();
21+
when constants_up.co_red then -- never reached
22+
my_package.do_black(); -- dead code
23+
else null;
24+
end case;
25+
end;
26+
/
27+
```
28+
29+
## Example (good)
30+
31+
``` sql
32+
declare
33+
l_color types_up.color_code_type;
34+
begin
35+
case l_color
36+
when constants_up.co_red then
37+
my_package.do_red();
38+
when constants_up.co_blue then
39+
my_package.do_blue();
40+
when constants_up.co_black then
41+
my_package.do_black();
42+
else null;
43+
end case;
44+
end;
45+
/
46+
```

0 commit comments

Comments
 (0)