Name

STYL-02: Adopt logical, consistent naming conventions for modules and data structures.

Synopsis

Adopt and promote standard ways to define names of program elements. Choose a level of “formality” of naming conventions based on your needs. If, for example, you have a team of two developers working on a small code base, you can probably get away with naming conventions that don’t go far beyond “use meaningful names.” If you are building a massive application involving dozens of developers, you probably need to define more comprehensive rules.

Here are some general recommendations for conventions:

  • Identify the scope of a variable in its name. A global variable can be prefaced with g_ , for example.

  • Use a prefix or suffix to identify the types of structures being defined. Consider, for example, declarations of TYPEs: of collections, objects, records, ref cursors, etc. A standard approach to declaring such a structure is <name>_t. Types are quite different from variables; you should be able to identify the difference with a glance.

  • Use the same case convention for user-defined types as the standard datatypes in order to help them stand out. Datatypes (built-in or user-defined) should follow a different casing rule from variables (such as all uppercase for types, lowercase for variables).

  • Use a readable format for your names. Since PL/SQL isn’t case-sensitive, the “camel notation” (as in minBalanceRequired), for example, is probably not a good choice for constructing names. Instead, use separators such as _ (underscore) to improve readability (as in min_balance_required). While names can be as long as 30 characters, keep them short, as well as readable.

  • Organize like items together. For example, declare record variables together in the same section. Declare all constants together in another section, separated from the previous section by whitespace.

It isn’t possible to provide a comprehensive list of naming conventions in this book. The particular conventions you choose, furthermore, aren’t nearly as important as the fact that you set some standard for naming conventions. See Section for downloadable style guides.

Example

Here is a block of code that reflects no standardization of naming conventions:

CREATE OR REPLACE PROCEDURE showborrowedbooks (
   date_borrowed IN DATE)
IS
   date_returned DATE := SYSDATE;
   mindaysborrowed INTEGER := 10;

   TYPE book_borrowed IS RECORD (
      dateborrowed DATE,
      daysborrowed INTEGER,
      isbn         book.isbn%TYPE,
      datedue      DATE);

   borrowedbook book_borrowed;

   CURSOR allborrowed IS
      SELECT * FROM borrowed_book
       WHERE returned = 'N';
BEGIN
   IF dateborrowed < datereturned
   THEN
      FOR rec IN allborrowed
      LOOP
         borrowedbook:= rec;

         IF borrowedbook.daysborrowed > mindaysborrowed
         THEN
            pl (borrowedbook.isbn);
         END IF;
      END LOOP;
   END IF;
END showborrowedbooks;

Here’s that same block of code based on standards. I use underscores in names; suffixes on parameters, records, and cursors; prefixes to show scope (l_ for local) and type (c_ for constant). Compare carefully the following item names with those in the previous example:

CREATE OR REPLACE PROCEDURE show_borrowed_books (
   date_borrowed_in IN DATE)
IS
   c_date_returned CONSTANT DATE := SYSDATE;
   l_min_days_borrowed INTEGER := 10;

   TYPE book_borrowed_rt IS RECORD (
      date_borrowed DATE,
      days_borrowed INTEGER,
      isbn          book.isbn%TYPE,
      date_due      DATE);

   borrowed_book_rec book_borrowed_rt;

   CURSOR all_borrowed_cur IS
      SELECT * FROM borrowed_book
       WHERE returned = 'N';
BEGIN
   IF date_borrowed_in < c_date_returned
   THEN
      FOR book_rec IN all_borrowed_cur
      LOOP
         borrowed_book_rec := book_rec;

         IF borrowed_book_rec.days_borrowed > 
            l_min_days_borrowed
         THEN
            pl (borrowed_book_rec.isbn);
         END IF;
      END LOOP;
   END IF;
END show_borrowed_books;

Now it’s possible to look at any individual part of show_borrowed_books and make sense of the different kinds of structures manipulated by the program.

Benefits

By setting standards, you don’t have to constantly worry about how to write your code. Concentrate on the important stuff: the business logic.

Developers come up to speed more quickly on the code base as they transfer into new groups; they also transfer their knowledge and productivity from one project to another.

Challenges

Define naming standards that are appropriate to your project (not overly rigid for the size of the team and complexity of the application).

Get developer buy-in for the conventions. One way to achieve this is to get the developers themselves to set those conventions.

Check for compliance with conventions (although this is difficult to do). You can build scripts in PL/SQL and SQL to analyze source code for conformance with some rules (e.g., “Don’t use fixed-length CHAR declarations.”). Currently, a comprehensive review must be performed manually; I hope that tools will become available in the next several years.

Resources

  1. See Steve McConnell’s http://www.construx.com site and his Code Complete book for naming convention suggestions.

  2. standards.doc : An unfinished draft of some naming and coding standards for PL/SQL developers; be sure to review and edit this document before using in your organization.

  3. standards.zip : An HTML-driven comprehensive guide to a set of naming conventions for PL/SQL code (courtesy of Matthew MacFarland).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset