Home
Jump statements should not be followed by dead code
Description
The "Jump Statements Should Not Be Followed by Dead Code" rule states that when a jump statement (such as a GOTO, EXIT, or RETURN statement) is used, the code that follows should not be executed. This is because the jump statement will cause the program to jump to a different part of the code, and any code after the jump statement will not be executed. This can lead to unexpected results and can be difficult to debug. Therefore, it is best practice to avoid having dead code after a jump statement.
Key Benefits
- Eliminates unnecessary code: Jump statements should not be followed by dead code as it eliminates unnecessary code which can slow down the program.
- Improves readability: By avoiding dead code, it improves the readability of the program, making it easier for developers to understand.
- Reduces complexity: Jump statements should not be followed by dead code as it reduces the complexity of the program, making it easier to debug and maintain.
Non-compliant Code Example
BEGIN
FOR i IN 1 .. CUSTOMER_TABLE.COUNT
LOOP
EXIT WHEN CUSTOMER_TABLE(i).Id > 25; --Non compliant code (Dead code after exit statement)
IF CUSTOMER_TABLE(i) IS NOT NULL THEN
DBMS_OUTPUT.PUT( i || ' = (' || CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
END IF;
END LOOP;
EXCEPTION
WHEN ERRORS THEN
RETURN;
END CUSTOMER_TABLE_ITERATION;
function GetCompleteCustomerDetails(FirstName in nvarchar2,
LastName in nvarchar2,
ADDRESS1 in nvarchar2,
ADDRESS2 in nvarchar2,
Area in nvarchar2,
City in nvarchar2)return nvarchar2
IS
BEGIN
RETURN(CONCAT(CONCAT(CONCAT(FirstName,LastName),CONCAT(ADDRESS1,ADDRESS2)),CONCAT(Area,City)));
CONCAT(CONCAT(ADDRESS1,ADDRESS2),CONCAT(Area,City)); --Non compliant code (Dead code after return statement)
END GetCompleteCustomerDetails;
Compliant Code Example
BEGIN
FOR i IN 1 .. CUSTOMER_TABLE.COUNT
LOOP
IF CUSTOMER_TABLE(i) IS NOT NULL THEN
DBMS_OUTPUT.PUT( i || ' = (' || CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
END IF;
EXIT WHEN CUSTOMER_TABLE(i).Id > 25; --Compliant code (No dead code after exit statement)
END LOOP;
EXCEPTION
WHEN ERRORS THEN
RETURN;
END CUSTOMER_TABLE_ITERATION;
function GetCompleteCustomerDetails(FirstName in nvarchar2,
LastName in nvarchar2,
ADDRESS1 in nvarchar2,
ADDRESS2 in nvarchar2,
Area in nvarchar2,
City in nvarchar2)return nvarchar2
IS
BEGIN
CONCAT(CONCAT(ADDRESS1,ADDRESS2),CONCAT(Area,City)); --Compliant code (No dead code after return statement)
RETURN(CONCAT(CONCAT(CONCAT(FirstName,LastName),CONCAT(ADDRESS1,ADDRESS2)),CONCAT(Area,City)));
END GetCompleteCustomerDetails;