 |
|
PL/SQL Spacing Standards
Oracle Tips by Jonathan Ingram
|
PL/SQL Horizontal Spacing Best
Practices Standards
You?ve probably heard of the obfuscated C contest, where the entrants attempt
to cram as much code as possible onto a single line that does some type of work
while remaining completely unreadable. If you have any experience maintaining
code, you?ve probably seen more than a few pieces of code that have a good
chance of winning a contest like this. The guidelines presented in this section
are an attempt to guide you away from writing hard-to-read code.
? One of the most
important elements in creating readable code is the spacing placed around
operators. Below shows common operators and keywords that need to be preceded
and followed by a space when they are used in expressions.
Operators
and keywords to be preceded and followed by a space in expressions. |
+ |
- |
* |
/ |
& |
< |
> |
= |
!= |
<= |
>= |
:= |
=> |
|| |
.. |
: |
<> |
IN |
OUT |
AND |
OR |
NOT |
NULL |
|
Often more than one of the operators and keywords shown in Table D.3 will be
adjacent to each other inside an expression. In this instance, it is recommended
that only one space lie between the two operators/identifiers. For example:
IF (vMajor IS NOT NULL) THEN
? Spaces should precede
and follow character (') literals.
SELECT first_name || ' ' || middle_name || ' ' || last_name
'student_name'
FROM STUDENTS
WHERE ssn = 999999999;
? Do not leave any blank
spaces preceding or following the ** operator.
nRaisedNum := nNum**nPower;
? Do not leave blank
spaces before or after the plus (+) and minus (-) signs when used as unary
operators.
nNumber := -nSecondNumber;
nNumber := +nSecondNumber;
? Do not use spaces
between multiple parentheses or semicolons (;). Always precede the first opening
parenthesis of a set with a space.
AND (((x < 5) AND (y < 5))
OR ((x > 5) AND (y > 5)));
PL/SQL Vertical Spacing Best Practices
Standards
Vertical spacing helps distance elements in the code from one another,
reducing the visual clutter above and below statements. To create appropriate
vertical spacing for your code, place a blank line in the locations described in
the following list:
? Before lines containing
the keywords IF, ELSE, ELSIF, and EXCEPTION. If the
line is preceded by a comment, place the blank line before the comment instead
of before the line of text.
--
-- If the student's grade point average meets the criteria for
-- mandatory academic counseling, add the student's name and social
-- security number to the list.
--
IF (nRealGPA < 1.5) THEN
<statements>
--
-- We also want to consider students who are failing two or more
-- classes, even if their GPA is above 1.5.
--
ELSIF Has_Two_Fails (nForSSN => nSSN) THEN
<statements>
ELSE
<statements>
END IF;
? Before any line
containing the LOOP keyword. Do not place a blank line before source code
containing the END LOOP keyword. (As with lines of code containing the
IF keyword, keep the comments for a line of code with the comment by placing
a blank line before the comment.)
--
-- For each student returned by the query, add the student's social
-- security number to the PL/SQL table.
--
FOR Students_rec IN Students_cur LOOP
<statements>
END LOOP;
? Before each exception
after the first declared within the EXCEPTION section of a PL/SQL block.
EXCEPTION
WHEN NO_DATA_FOUND THEN
<statements>
WHEN TOO_MANY_ROWS THEN
<statements>
WHEN OTHERS THEN
<statements>
? Before and after the
variable, constant, and type declarations for a PL/SQL block.
PROCEDURE Update_Student_GPA (nSSN IN number)
IS
<declaration>
<declaration>
BEGIN
<statements>;
END Update_Student_GPA;
? Following the
declaration of the procedure and its parameters.
PROCEDURE Update_Student_GPA (nSSN IN number)
IS
? Do not place an empty
line before a line containing the END IF keyword. Do place blank lines
after the last line of code containing the END IF keyword.
IF (some expression) THEN
IF (some expression) THEN
IF (some expression) THEN
<statements>
END IF;
END IF;
END IF;
<statements>
This is an excerpt from "High Performance Oracle Database
Automation", by Jonathan Ingram and Donald K. Burleson, Series Editor.