Home

GOTO should not be used to jump backwards

Description

    The Pl_Sql code rule "GOTO should not be used to jump backwards" states that the GOTO statement should not be used to jump to a previous line of code. This is because it can lead to code that is difficult to read and maintain, and can cause unexpected results. Instead, other control flow statements such as IF-THEN-ELSE, WHILE, and FOR should be used to control the flow of the program.

Key Benefits

  • Time Saving: GOTO should not be used to jump backwards as it is a time consuming process as the code has to be read multiple times to understand the flow.
  • Readability: GOTO should not be used to jump backwards as it makes the code difficult to read and understand, making it difficult to debug and maintain.
  • Efficiency: GOTO should not be used to jump backwards as it can lead to inefficient code, as it can cause the code to be executed multiple times unnecessarily.

 

Non-compliant Code Example

DECLARE
  result PLS_INTEGER := 0;
  cnt PLS_INTEGER := 1;
BEGIN
   <<get_result>>
  result := result + cnt;
  cnt := cnt + 1;

  IF cnt <= 5 THEN
    GOTO get_result;  --Non compliant code
  END IF;

  DBMS_OUTPUT.PUT_LINE('Sum from 1 to 5 is ' || result); 
END;

Compliant Code Example

DECLARE
  result PLS_INTEGER := 0;
BEGIN
  FOR cnt IN 1 .. 5 LOOP --Compliant code
    result := result + cnt;
  END LOOP;

  DBMS_OUTPUT.PUT_LINE('Sum from 1 to 5 is ' || result);  
END;
Visual Expert 2024
 VEPLSQLRULE20