Index: openacs-4/packages/acs-core-docs/www/eng-standards-plsql.html =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-core-docs/www/eng-standards-plsql.html,v diff -u -r1.2 -r1.3 --- openacs-4/packages/acs-core-docs/www/eng-standards-plsql.html 17 Oct 2001 20:39:25 -0000 1.2 +++ openacs-4/packages/acs-core-docs/www/eng-standards-plsql.html 2 Feb 2002 03:47:32 -0000 1.3 @@ -1,37 +1,79 @@ - -9. PL/SQL Standards

Home : Documentation : Part III. For ACS Developers : 6. Engineering Standards : 9. PL/SQL Standards 

9. PL/SQL Standards

Table of Contents

9.1. General
9.2. Code
9.3. Style

+ + + + +PL/SQL Standards + + + + + + + + +

+
+

+PL/SQL Standards

+

By richardl@arsdigita.com and yon@arsdigita.com -

- Like any other part of the ACS, PL/SQL code must be maintainable and - professional. This means that it must be consistent and therefore - must abide by certain standards. The standards will ensure that our - product will be useful long after the current people building and - maintaining it are around. Following are some standards and - guidelines that will help us achieve this goal: -

9.1. General

  1. +

+

+ Like any other part of the OpenACS, PL/SQL (or pl/pgsql) code must be + maintainable and professional. This means that it must be consistent and + therefore must abide by certain standards. The standards will ensure that + our product will be useful long after the current people building and + maintaining it are around. Following are some standards and guidelines + that will help us achieve this goal: +

+
+

+General

+
    +
  1. All PL/SQL code must be well documented. We must write code that is maintainable by others, this is especially true in our case because we are building an open source toolkit than anyone can download and browse the source code. So document like you are - trying to impress your "Introduction to Programming" professor or + trying to impress your "Introduction to Programming" professor or TA. -

  2. +

  3. +
  4. It is important to be consistent throughout an application as much as is possible given the nature of team development. This means carrying style and other conventions suchs as naming within an application, not just within one file. -

9.2. Code

  1. +

  2. +
+
+
+

+Code

+
    +
  1. Encapsulation of related fuctionality is key to maintainability and upgradeability of our software. Try to bundle your code into packages whenever possible. This will make upgrading, bug fixing, and customizing, among other things, a possibility. -

  2. +

  3. +
  4. +

    When creating functions or procedures use the following template, it demonstrates most of the guidelines set forth in this document that correspond to functions and procedures: -

    +    

    +
      
             create or replace procedure|function <proc_or_func_name> (
                      <param_1>    in|out|inout <datatype>,
    @@ -51,24 +93,31 @@
             /
             show errors
          
    -
  5. + +

  6. +
  7. Always use create or replace procedure|function <proc_or_func_name>. It makes reloading packages much easier and painless to someone who is upgrading or fixing a bug. -

  8. +

  9. +
  10. Always qualify end statements, i.e., the end statement for a package should be end <package_name>;, not just end;; same goes for procedures, functions, package bodies, and triggers. -

  11. - Always use the "show errors" SQL*Plus command after each PL/SQL +

  12. +
  13. + Always use the "show errors" SQL*Plus command after each PL/SQL block. It will help you debug when there are compilation errors in your PL/SQL code. -

  14. +

  15. +
  16. +

    Name parameters as simply as possible, i.e., use the column name if the parameter corresponds to a table column. We're deprecating the v_* and *_in syntax in favor of named parameters notation: -

    +    

    +
     
           
             acs_user.create(first_names => 'Jane', last_name => 'Doe', etc.)
    @@ -78,14 +127,16 @@
             acs_user.create(first_names_in => 'Jane', last_name_in => 'Doe', etc.)
           
          
    -

    + +

    To achieve this we must fully qualify arguements passed into procedures or functions when using them inside a SQL statement. This will get rid of any ambiguities in your code, i.e. it will tell the parser when you want the value of the column and when you want the value from the local variable. Here is an example: -

    +

    +
     
             create or replace package body mypackage 
                 .
    @@ -95,7 +146,7 @@
                     .
                     delete
                       from parties
    -                 where party_id = myproc.party_id;
    +                 where party_id = myproc.party_id;
                     .
                     .
                 end myproc;
    @@ -105,21 +156,29 @@
             /
             show errors
          
    -
  17. - Explicitly designate each parameter as "in," "out," or "inout." -

  18. + +

  19. +
  20. + Explicitly designate each parameter as "in," "out," or "inout." +

  21. +
  22. Each parameter should be on its own line, with a tab after the parameter name, then in/out/inout, then a space, and finally the datatype. -

  23. +

  24. +
  25. Use %TYPE and %ROWTYPE whenever possible. -

  26. - Use 't' and 'f' for booleans, not the PL/SQL "boolean" datatype +

  27. +
  28. + Use 't' and 'f' for booleans, not the PL/SQL "boolean" datatype because it can't be used in SQL queries. -

  29. +

  30. +
  31. +

    All new functions (e.g., acs_object.new, party.new, etc.) should optionally accept an ID: -

    +    

    +
     
           
             create or replace package acs_object
    @@ -134,20 +193,61 @@
                ) return acs_objects.object_id%TYPE;
          
         
    -

    + +

    takes the optional argument object_id. Do this to allow people to use the same API call when they are doing double click protection, that is, tehy have already gotten an object_id and now they want to create the object with that object_id. -

9.3. Style

+

+ +
+
+
+

+Style

+

Some general style guidelines to follow for the purpose of consistency across applications. -

  1. +

    +
      +
    1. Standard indentation is 4 spaces. Our PL/SQL code is not only viewable in the SQL files but also through our SQL and PL/SQL browsers. This means that we should try to make it as consistent as possible to all source code readers. -

    2. +

    3. +
    4. Lowercase everything, with the exception of %TYPE and %ROWTYPE. -

    ($Id$)

+

+
+

($Id$)

+
+
+ + +