Home

GOTO statements should not be used

Description

    The GOTO statements should not be used rule in PL/SQL code states that the GOTO statement should not be used to control the flow of a program. This statement can lead to code that is difficult to read and maintain, and can cause unexpected results. Instead, PL/SQL code should use IF-THEN-ELSE, CASE, and LOOP statements to control the flow of a program.

Key Benefits

  • Eliminates Complexity: GOTO statements can make code difficult to read and understand, leading to potential errors and bugs.
  • Improves Readability: By avoiding GOTO statements, code can be written in a more logical and straightforward manner, making it easier to read and debug.
  • Increases Efficiency: Programs that are written without GOTO statements are often more efficient, as they require fewer instructions to execute.

 

Non-compliant Code Example

DECLARE
  first_name  VARCHAR2(25);
  customer_id     NUMBER(6) := 120;
BEGIN
 
  
  BEGIN
    DBMS_OUTPUT.PUT_LINE (first_name);
    customer_id := customer_id + 1;
 
    IF customer_id < 200 THEN
      GOTO get_customer_name;  --Non compliant code (GOTO statement should not be used)
    END IF;

    <<get_customer_name>>
      SELECT FIRSTNAME INTO first_name
      FROM CUSTOMERS
      WHERE Id = customer_id;
  END;
END;

Compliant Code Example

DECLARE
    first_name  VARCHAR2(25);
    customer_id NUMBER(6) := 120;
BEGIN
    BEGIN
        DBMS_OUTPUT.PUT_LINE(first_name);
        customer_id:= customer_id + 1;

        IF customer_id< 200 THEN            --Compliant code (GOTO statement is not used)
            SELECT FIRSTNAME INTO first_name
            FROM CUSTOMERS
            WHERE Id = customer_id;
        END IF;
    END;
END;
Visual Expert 2024
 VEPLSQLRULE66