Skip to content

G-4210: Try to use CASE rather than an IF statement with multiple ELSIF paths.

Minor

Maintainability, Testability

Reason

if statements containing multiple elsif tend to become complex quickly.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
declare
   l_color types_up.color_code_type;
begin
   if l_color = constants_up.co_red then
      my_package.do_red();
   elsif l_color = constants_up.co_blue then
      my_package.do_blue();
   elsif l_color = constants_up.co_black then
      my_package.do_black();
   end if;
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
declare
   l_color types_up.color_code_type;
begin
   case l_color
      when constants_up.co_red then
         my_package.do_red();
      when constants_up.co_blue then
         my_package.do_blue();
      when constants_up.co_black then
         my_package.do_black();
      else
         null;
   end case;
end;
/