Name

CTL-11: Label and highlight GOTOs if using this normally unnecessary construct.

Synopsis

I suppose that it was thorough of Oracle to include a GOTO statement in the PL/SQL language. This statement, however, should generally be avoided, as it leads to unstructured code design that is hard to analyze and debug.

There are scenarios in which a GOTO can be justified; these mostly relate to going into existing spaghetti code to fix a bug or enhance the code. For an extensive review of GOTO-related issues, see Chapter 16 in Steve McConnell’s book, Code Complete.

Example

Here is a use of GOTO that calls attention to itself:

CREATE OR REPLACE PROCEDURE someone_elses_mess
/*
|| Author: Longgone Consultant
|| Maintained by: Sad Employee
||
|| Modification History
|| When    Who    What
|| --------------------------------------------
|| 11/2000 Sad E. Fixed bug in overdue logic.
||                Used GOTO to bypass Gordian 
||                Knot of code left by L.C.
*/
IS
BEGIN
   IF ... THEN
      IF ... THEN
         FOR rec IN cur LOOP
            -- 11/2000 Bypass with GOTO
            GOTO <<quick_exit>>
         END LOOP;
         ... lots more code
      END IF;
      -- 11/2000 GOTO Target
      <<quick_exit>>
   END IF;

Benefits

Even if you can, at times, justify the use of a GOTO, you can almost always achieve the same effect with a more structured and more easily understood use of conditional and loop logic.

Resources

Code Complete, by Steve McConnell: See Chapter 16, Unusual Control Structures, for an in-depth discussion of the GOTO statement and recommendations for when it can justifiably be used. ...

Get Oracle PL/SQL Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.