Oracle PL/SQL Programming

advertisement
Oracle PL/SQL Programming
The Power
of Names
Steven Feuerstein
steven.feuerstein@software.dell.com
PL/SQL Evangelist, Dell
Oracle PL/SQL Programming
What's In a Name?
• A name is an abstraction, a symbol that is
associated with an underlying set of data.
• A common concept in history is: knowing the
"true name" of something or someone gives
one power over that thing.
– Le Guin's Earthsea cycle a classic example
– Let's not forget about Rumplestiltskin.
– And if you know God's true name…wow!
• Names exist in a hierarchy of abstraction.
– Human -> Woman -> Mother -> Joan Feuerstein
Copyright 2013 Feuerstein and Associates
Page 2
Oracle PL/SQL Programming
What's in a brain?
• Humans constantly seek models to describe how
the brain works.
– Most have failed dismally. Where art thou, AI?
– Most would agree that the brain identifies and stores
data, which can be referenced by name.
• Seems fairly clear that our brain is a big pattern
analysis engine, sorting through all the data our
senses provide.
– Build plans of action based on those patterns.
– One interesting consequence: stereotyping/prejudice
unavoidable feature of "being human"?
– Check out: numenta.org and On Intelligence.
Copyright 2013 Feuerstein and Associates
Page 3
Oracle PL/SQL Programming
Pattern Analysis and Abstraction
• Or to bring it back to our favorite species….
Everyone
knows you're
a person
Copyright 2013 Feuerstein and Associates
Page 4
Oracle PL/SQL Programming
Names in Software
• Names are crucial to human efforts to
transform our world to make it more
convenient and more comfortable.
– But what do they do for software?
• Names are used in software in two key ways….
– 1. Hide information: avoid information overload!
– 2. Improve maintainability: make it easier to read
and understand code.
Copyright 2013 Feuerstein and Associates
Page 5
Oracle PL/SQL Programming
Hide Information
• Information hiding: a key principle of software
generally and object orientation in particular.
• Human brains can handle only so much data.
– We live in an age of information overload
(internet, 24 hour TV, etc.).
• This is certainly true of complex software.
– The closest humans come to "world creators."
• So we hide details that are not needed at this
moment behind a name.
Copyright 2013 Feuerstein and Associates
Page 6
Oracle PL/SQL Programming
Improve Maintainability of Code
• Arguably the most important (and most
neglected) aspect of software.
• Factors in maintainability include:
– Self-documentation: comments are bad. :-)
– Single point of definition: repetition is bad.
– Ease (and associated low cost) of change over
time: spaghetti code is bad.
• Let's take a look at features of PL/SQL that
relate to names.
Copyright 2013 Feuerstein and Associates
Page 7
Oracle PL/SQL Programming
Where Names Are Found in PL/SQL
• Where are they not found?
– Software without names is, well, blobby.
•
•
•
•
•
Tables, views, columns
Program units and their subprograms
Types…and subtypes
Variables, constants and cursors
Labels for "unnamed" sections of code
Copyright 2013 Feuerstein and Associates
Page 8
Oracle PL/SQL Programming
Guidelines for An Excellent PL/SQL Name
• Has no more than 30 characters. Bummer!
– And finds a nice balance between length and clarity
• Does not rely on CamelNotation.
– Don't fight case insensitivity of SQL-PL/SQL!
• Reflects the information hidden behind the
name.
– A misleading name makes reading code extremely
difficult.
• Standardize the structure of the name.
– Prefixes, suffixes, singular vs. plural
get_the_name_right.sql
Copyright 2013 Feuerstein and Associates
Page 9
Oracle PL/SQL Programming
Program Units and Names
• The starting point (leaving aside
tables and views) for all PL/SQL
developers.
• Start with package names, then
subprogram names, then
nested subprogram names.
– Package names: define an area of
functionality, into which are
collected multiple procedures
and functions.
– Subprogram names: procs do
things, functions return things.
Don't put "get" in function name!
Copyright 2013 Feuerstein and Associates
Page 10
Oracle PL/SQL Programming
Nested Subprograms
"extreme" modularization -> high readability
• You can declare a procedure or
function in any declaration
section.
– An anonymous block
– Another procedure or function
• Nested subprograms offer
tremendous power
to improve readability
through information
hiding.
Copyright 2013 Feuerstein and Associates
"table of contents" for subprogram
Page 11
Oracle PL/SQL Programming
Types and Subtypes
• Oracle defines lots of "generic" types for us to
use (NUMBER, VARCHAR2,
DBMS_SQL.NUMBER_TABLE, etc.).
• We can also create our own types, as well as
the simple but powerful SUBTYPE.
– Declare types in package specs so they can be reused easily.
– Subtypes give application-specific names for
commonly-used declaration types.
Copyright 2013 Feuerstein and Associates
plsql_limits.pks
string_tracker3.*
Page 12
Oracle PL/SQL Programming
About SUBTYPEs
• You can't always use %TYPE or %ROWTYPE in your
declaration.
• You can, however, always define a "subtype" or
subset of an existing type with the SUBTYPE
statement. SUBTYPE benefits:
– Avoid exposing and repeating constraints.
– Give application-specific names to types. Critical when
working with complex structures like collections of
records, and nested collections.
– Apply constraints, such as numeric ranges, to the variable
declared with the subtype.
Copyright 2013 Feuerstein and Associates
Page 13
Oracle PL/SQL Programming
Applying SUBTYPEs
• Two key scenarios:
– Whenever you are about to write a VARCHAR2(N)
or other constrained declaration, define a subtype
instead, preferably in a package specification.
– Instead of writing a comment explaining a
declaration, put the explanation into a subtype.
Instead
of this:
Write
this:
DECLARE
l_full_name VARCHAR2(100);
l_big_string VARCHAR2(32767);
DECLARE
l_full_name employees_rp.full_name_t;
l_big_string plsql_limits.maxvarchar2;
Copyright 2013 Feuerstein and Associates
fullname.pks
plsql_limits.pks
string_tracker3.*
Page 14
Oracle PL/SQL Programming
Variables, Constants and Cursors
• As with program units, make sure your names
reflect the content/usage of your variable.
• Use constants to hide literal values, providing
a single point of definition.
• You can also declare a cursor in a package
specification (a named query, in essence).
– But watch out! The cursor will persist in your
session - you must close it explicitly when done.
Copyright 2013 Feuerstein and Associates
hardcoding.sql
no_more_hardcoding.sql
pkgcur.pkg
Page 15
Oracle PL/SQL Programming
Labels for "Unnamed" Code Sections
• Nested blocks are "anonymous".
• Loops are "anonymous."
• But you can give them names with labels!
– But I recommend that you make them nested
subprograms instead.
– Use Toad's Refactoring tool to make sure you "do it right."
BEGIN
<<per_month_report>>
FOR indx IN 1 .. 12 LOOP
...
END LOOP per_month_report;
<<close_orders>>
DECLARE
l_new_local_var NUMBER := 100;
BEGIN
...
END close_orders;
Copyright 2013 Feuerstein and Associates
Page 16
Oracle PL/SQL Programming
The Power of Names
• Names contain an enormous amount of
explanatory and manipulative power.
• With great power, comes great responsibility.
– Thanks, Spiderman!
• So first maximize opportunities to replace
complexity with a name.
• Next, choose your names with care.
• The result? Sparkling clear software that will
be given the name: "Excellent Stuff"
Copyright 2013 Feuerstein and Associates
Page 17
Oracle PL/SQL Programming
Visit and Use…
• A portal for Toad users specifically and Oracle
technologists generally.
– OK, and not just Oracle!
• An overload of information (blogs, whitepapers,
forums, utilities, etc.) carefully hidden behind lots
of names.
• PL/SQL Obsession: toadworld.com/SF, my
cyberhome for all things PL/SQL!
Copyright 2013 Feuerstein and Associates
Page 18
Oracle PL/SQL Programming
Other Resources for PL/SQL Developers
www.plsqlchallenge.com
Daily PL/SQL quiz, weekly SQL,
APEX and logic quizzes. Prizes!
www.plsqlchannel.com
27+ hours of detailed video
training on Oracle PL/SQL
www.stevenfeuerstein.com
Sign up for monthly PL/SQL
newsletter
Copyright 2013 Feuerstein and Associates
Page 19
Download