Home

PACKAGE BODY initialization sections should not contain RETURN statements

Description

    The rule "PACKAGE BODY initialization sections should not contain RETURN statements" states that the initialization sections of a PL/SQL package body should not contain RETURN statements. This rule is important because RETURN statements are used to terminate a PL/SQL program, and having them in the initialization sections of a package body could lead to unexpected behavior. Additionally, RETURN statements should only be used when necessary, as they can be difficult to debug.

Key Benefits

  • No unintended side effects: PACKAGE BODY initialization sections should not contain RETURN statements as this can lead to unexpected side effects, such as a procedure not being called when it should be.
  • Simplified debugging: By avoiding RETURN statements in PACKAGE BODY initialization sections, debugging is simplified as the debugger can easily identify which procedures are being called.
  • More efficient execution: By not containing RETURN statements, PACKAGE BODY initialization sections are more efficient as the code is executed faster.

 

Non-compliant Code Example

CREATE OR REPLACE PACKAGE BODY CUSTOMER_PACKAGE 
AS
 	function GetCompleteCustomerDetails(customerId In INTEGER)
		return CUSTOMER_T 
		Is
		BEGIN
			Select FIRSTNAME, LASTNAME, AREA, CITY Into CUSTOMER_T.FirstName, CUSTOMER_T.LastName, CUSTOMER_T.Area, CUSTOMER_T.City FROM CUSTOMERS Where Id = customerId;
			RETURN CUSTOMER_T; 
	END GetCompleteCustomerDetails;

	BEGIN
		DBMS_OUTPUT.PUT('Loading  CUSTOMER_PACKAGE');
		RETURN;         --Non compliant code (PACKAGE BODY initialization sections contain RETURN statements)
		DBMS_OUTPUT.PUT('Junk code CUSTOMER_PACKAGE');
		Select FIRSTNAME, LASTNAME, AREA, CITY FROM CUSTOMERS;
END CUSTOMER_PACKAGE

Compliant Code Example

CREATE OR REPLACE PACKAGE BODY CUSTOMER_PACKAGE 
AS
 	function GetCompleteCustomerDetails(customerId In INTEGER)
		return CUSTOMER_T 
		Is
		BEGIN
			Select FIRSTNAME, LASTNAME, AREA, CITY Into CUSTOMER_T.FirstName, CUSTOMER_T.LastName, CUSTOMER_T.Area, CUSTOMER_T.City FROM CUSTOMERS Where Id = customerId;
			RETURN CUSTOMER_T; 
	END GetCompleteCustomerDetails;

	BEGIN --Compliant code
		DBMS_OUTPUT.PUT('Loading  CUSTOMER_PACKAGE');
		DBMS_OUTPUT.PUT('Junk code CUSTOMER_PACKAGE');
		Select FIRSTNAME, LASTNAME, AREA, CITY FROM CUSTOMERS;
END CUSTOMER_PACKAGE
Visual Expert 2024
 VEPLSQLRULE55