G-4375: Always use EXIT WHEN instead of an IF statement to exit from a loop.
Reason
If you need to use an exit
statement use its full semantic to make the code easier to understand and maintain. There is simply no need for an additional if
statement.
Example (bad)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | declare
co_first_year constant pls_integer := 1900;
begin
<<process_employees>>
loop
my_package.some_processing();
if extract(year from sysdate) > co_first_year then
exit process_employees; -- violates also G-4365
end if;
my_package.some_further_processing();
end loop process_employees;
end;
/
|
Example (good)
1
2
3
4
5
6
7
8
9
10
11
12
13 | declare
co_first_year constant pls_integer := 1900;
begin
<<process_employees>>
loop
my_package.some_processing();
exit process_employees when extract(year from sysdate) > co_first_year;
my_package.some_further_processing();
end loop process_employees;
end;
/
|