G-2160: Avoid initializing variables using functions in the declaration section.
Critical
Reliability
Reason
If your initialization fails, you will not be able to handle the error in your exceptions block.
Example (bad)
DECLARE
co_department_id CONSTANT INTEGER := 100;
l_department_name departments.department_name%TYPE :=
department_api.name_by_id(in_id => co_department_id);
BEGIN
sys.dbms_output.put_line(l_department_name);
END;
/
Example (good)
DECLARE
co_department_id CONSTANT INTEGER := 100;
co_unkown_name CONSTANT departments.department_name%TYPE := 'unknown';
l_department_name departments.department_name%TYPE;
BEGIN
<<init>>
BEGIN
l_department_name := department_api.name_by_id(in_id => co_department_id);
EXCEPTION
WHEN value_error THEN
l_department_name := co_unkown_name;
END init;
sys.dbms_output.put_line(l_department_name);
END;
/