Hard to Get

--人生在世 难得二字

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

PL/SQL Best Practices [6][Data Structures]-Naming Identifiers

Employ naming conventions throughout your applications. For example:

ExamplesCursors: company_cur
Constants: c_earliest_date
Variables: v_company_id
Parameters: p_company_id (Append parameter mode: company_id_in)
Records and Record Types: company_rec tot_sales_rectype

Standard prefixes or suffixes for cursor and record names, etc.
Avoid dozens of variations for the same identifier.
Even if in different programs, there is no reason to have different abbreviations and interpretations.
If the variable represents database information, incorporate the name of the database entity.
Take advantage of database standards as much as possible.

Do not, however, declare variables which have exactly the same name as a column. For example:

       

ExamplesAvoid Giving Different Elements the Same Name
You could name a variable the same as its column. But what happens when you need to use both in the same area of code?

The original version -- does the job:

1PROCEDURE clear_ledger
2   (store_in IN INTEGER)
3IS
4BEGIN
5   DELETE FROM store_ledger
6    WHERE store_nu = store_in
7      AND reg_cd = :GLOBAL.reg_cd;
8END;

The new and the ugly:

 1PROCEDURE clear_ledger
 2   (store_in IN INTEGER)
 3IS
 4   reg_cd VARCHAR2(2) :=:GLOBAL.reg_cd;
 5BEGIN
 6   DELETE FROM store_ledger
 7    WHERE store_nu = store_in
 8      AND reg_cd = reg_cd;
 9END;
10

We suggest adding an identifier which ensures that although the column name is similar, the variable name is uniquely different. For example:

1DECLARE
2    v_first_name TABLE.first_name%TYPE;
3    c_last_name CONSTANT TABLE.last_name%TYPE := 'Smith';
4

It is important to use your variable naming conventions throughout all code.

posted on 2005-09-12 22:11  Del  阅读(125)  评论(0)    收藏  举报