Oracle PL/SQL Best Practice 2.1 Overview: 1.1 Scope
Oracle PL/SQL Best Practice 2.1 Overview: 1.1 Scope
2.1 Overview
1.1 Scope
This document defines the standards and guidelines that will be used by developers when
submitting PL/SQL components. These guidelines will also be used by reviewers to determine
whether a component submission meets standards.
2.2 Standards
2.3 Packages and Procedures
All PL/SQL functions and procedures will be implemented as part of a package. The package names
used will be specified in the component specification.
A header should appear at the start of any script, procedure, function, package body, or package
spec. Consider this template header:
-- *****************************************************************
-- Description: Describe the purpose of the object. If necessary,
-- describe the design of the object at a very high level.
--
-- Input Parameters:
--
-- Output Parameters:
--
-- Error Conditions Raised:
--
-- Author: <your name>
--
-- Revision History
-- Date Author Reason for Change
-- ----------------------------------------------------------------
-- 03 JAN 2015 J.Schmoe Created.
-- *****************************************************************
If your code has a complex equation or formula that is expressed using a single statement or
operation, consider breaking the code into several smaller statements to make the operations less
intimidating. The equation will be much easier to debug; in addition, the process of breaking the
equation into subsections will increase your awareness of any mistakes that you make.
1
2.6 Indentation
Indentation will be 0 spaces for the outermost block and 3 spaces from the indentation level of each
enclosing block.
2.7 Alignment
Comma separated lists will be ‘stacked’ and aligned as shown below:
SELECT SUM(A)
,B
,C
FROM TABLE1
WHERE B = 5
GROUP BY B
,C
Example:
THEN
l_new_max := true;
THEN
l_new_max := false;
END IF;
2
g_ Package global variables
Arrange series of statements containing similar operators into columns whenever it will not
cause excessive white space and you have sufficient room to do so.
Correct:
vFirstName := 'Roger';
vLastName := 'Smith';
vSSN := 999999999;
Incorrect:
vFirstName := 'Roger';
vLastName := 'Smith';
vSSN := 999999999;
Always use parentheses in expressions containing more than one identifier or literal. This
clarifies code for inexperienced developers who are not familiar with operator precedence
and helps eliminate the possibility that you have overlooked something in your equation.
Correct:
Incorrect:
Align the IN and OUT keywords in columns when defining the interface for a procedure or
function.
Correct:
3
PROCEDURE Days_Between (dStartDate IN date,
dEndDate IN date,
nGPA IN OUT number,
nDaysBetween OUT number)
<procedure declarations and body>
Incorrect:
When calling a procedure or function, align the parameters into a column. This reduces the
visual clutter around the call, making it stand out from the rest of the code.
Correct:
Incorrect:
Packages should be named in accordance with the general purpose of the procedures and
functions contained within the package. For instance, a package containing routines used to
calculate a student's GPA would be named GPA_Calculations.
All documentation pertaining to system design should follow the conventions identified in
this standard. This applies especially to pseudocode that is used to document stored PL/SQL
objects.
4
2.14 Encapsulation
DML Statements
Insert and Update statements will be encapsulated in a single package procedure or function.
This will allow all inserts and updates to use identical SQL which will decrease statement parsing.
Conditionals
The ‘ELSIF/ELSE’ construct will be used in preference to multiple or nested conditionals.
Example:
These statements:
IF l_count_1 = l_count_2
THEN
// handle case 1
END IF;
THEN
// handle case 2
ELSE
THEN
// handle case 3
END IF;
END IF;
IF l_count_1 = l_count_2
THEN
// handle case 1
THEN
// handle case 2
5
THEN
// handle case 3
END IF;
Loop Exits
It’s considered bad practice to exit from the middle of a loop. Each loop should include only a single
exit point.
GOTO Statement
PL/SQL’s GOTO statement will not be used.
2.16 Exceptions
Raising Exceptions
PL/SQL components will not raise exceptions directly. The component will use a single procedure to
encapsulate exception raising logic. Exceptions will be used to indicate errors, not as a normal
method for branching control.
Custom Exceptions
Custom exceptions will be declared in the package specification. Custom exceptions will not hide an
underlying exception. For example, the developer should not catch a system-created exception and
then raise an application exception instead.
Exception Handling
Exception handling code will catch specific exceptions and will only use the ‘WHEN OTHERS’
construct in the outermost code. Every effort should be made to preserve the root cause of
exceptions to the client code.
2.17 Cursors
Declarations
Multi-row cursors are often useful to users outside the package and should be included in the
package specification at the designer’s discretion.
Fetching
Cursors will be fetched into cursor records, not into multiple variables.
6
2.18 Functions
Boolean Functions
The only valid values that may be returned from Boolean functions are ‘true’ and ‘false’. NULL
should never be returned from a Boolean function. Boolean functions may raise exceptions.
OUT Parameters
Functions should not contain ‘OUT’ parameters.