|
| 1 | +# G-7240: Never use RETURN in package initialization block. |
| 2 | + |
| 3 | +!!! tip "Minor" |
| 4 | + Maintainability |
| 5 | + |
| 6 | +## Reason |
| 7 | + |
| 8 | +The purpose of the initialization block of a package body is to set initial values of the global variables of the package (initialize the package state). Although `RETURN` is syntactically allowed in this block, it makes no sense. If it is the last keyword of the block, it is superfluous. If it is not the last keyword, then all code after the `RETURN` is unreachable and thus dead code. |
| 9 | + |
| 10 | +## Example (bad) |
| 11 | + |
| 12 | +``` sql |
| 13 | +create or replace package body employee_api as |
| 14 | + g_salary_increase types_up.sal_increase_type(4,2); |
| 15 | + |
| 16 | + procedure set_salary_increase (in_increase in types_up.sal_increase_type) is |
| 17 | + begin |
| 18 | + g_salary_increase := greatest(least(in_increase |
| 19 | + ,constants_up.max_salary_increase()) |
| 20 | + ,constants_up.min_salary_increase()); |
| 21 | + end set_salary_increase; |
| 22 | + |
| 23 | + function salary_increase return types_up.sal_increase_type is |
| 24 | + begin |
| 25 | + return g_salary_increase; |
| 26 | + end salary_increase; |
| 27 | + |
| 28 | +begin |
| 29 | + g_salary_increase := constants_up.min_salary_increase(); |
| 30 | + |
| 31 | + return; |
| 32 | + |
| 33 | + set_salary_increase(constants_up.min_salary_increase()); -- dead code |
| 34 | +end employee_api; |
| 35 | +/ |
| 36 | +``` |
| 37 | + |
| 38 | +## Example (good) |
| 39 | + |
| 40 | +``` sql |
| 41 | +create or replace package body employee_api as |
| 42 | + g_salary_increase types_up.sal_increase_type(4,2); |
| 43 | + |
| 44 | + procedure set_salary_increase (in_increase in types_up.sal_increase_type) is |
| 45 | + begin |
| 46 | + g_salary_increase := greatest(least(in_increase |
| 47 | + ,constants_up.max_salary_increase()) |
| 48 | + ,constants_up.min_salary_increase()); |
| 49 | + end set_salary_increase; |
| 50 | + |
| 51 | + function salary_increase return types_up.sal_increase_type is |
| 52 | + begin |
| 53 | + return g_salary_increase; |
| 54 | + end salary_increase; |
| 55 | + |
| 56 | +begin |
| 57 | + g_salary_increase := constants_up.min_salary_increase(); |
| 58 | +end employee_api; |
| 59 | +/ |
| 60 | +``` |
0 commit comments