PeopleSoft/SQR technical development PeopleSoft/SQR technical development PeopleSoft/SQR technical development
• Mail/phone
  Search
  Keyword:
  Tools
SQR Debugger
 
 
 
TECH TALK
Lightly technical observations on PeopleSoft and related topics
 
 
  Recent Topics


05/07/2008
Ignoring component-changed state


04/30/2008
App Engine


04/02/2008
Commenting code


03/05/2008
The "Powered By" page


01/30/2008
Formats in PeopleCode variables


01/09/2008
Page layout with group boxes


12/19/2007
Meta-SQL wrappers


12/05/2007
Derived record PeopleCode


11/28/2007
A Cancel button


11/07/2007
"Leading" the test effort


10/31/2007
Temporary tables in SQR


10/24/2007
Updating long character data


10/17/2007
Record.Field specification


10/03/2007
RELLANG.SQC


09/26/2007
Migrating project definitions


09/19/2007
Derived records


08/29/2007
Positioning elements on pages


08/22/2007
Thanks


08/08/2007
SQR "code alerts"


08/01/2007
Styles in HTML Areas


07/25/2007
Dynamic SQL for performance?


07/18/2007
Page Field Name


06/27/2007
PeopleCode event order


06/06/2007
Formatting Excel files using HTML


05/23/2007
Component-changed state


05/16/2007
Selecting styles for page fields


05/02/2007
Sending email from SQR


04/25/2007
Contractor networking


04/18/2007
Project X--and another project


04/11/2007
Effective and data entry dates


04/04/2007
A PeopleSoft home page


03/21/2007
Hyperion and the future of SQR


03/14/2007
The data doesn't lie



More in the
Archives

 

July 6, 2004

SQR Code Formatting

Unlike PeopleCode, SQR does not force a particular coding format. Sometimes I wish it did. You know the type:

      BEGIN-PROCEDURE CALC-AMT
      BEGIN-SELECT
      COMPRATE &COMPRATE
      COMP_FREQUENCY &COMP_FREQUENCY
       LET #COMPRATE=&COMPRATE
        LET $COMP_FREQ =  &COMP_FREQUENCY
          DO CALC-AMT-2 !CALCULATE AMOUNT
      END-SELECT
      END-PROCEDURE
      BEGIN-PROCEDURE CALC-AMT-2
      DISPLAY 'CALC-AMT-2'
      IF $COMP_FREQ = 'M'
      SHOW 'M'
      LET #F = 12 !SET F TO 12
      ELSE
       IF $COMP_FREQ = 'Q'
           SHOW 'Q'
                 LET #F = 4
               MOVE 1.0005 TO #NUM
           END-IF
         END-IF
         SHOW #NUM
      LET ...

This example has it all: Inconsistent or no indentation, useless comments, mysterious uncommented literals, DISPLAYs and SHOWs for debugging purposes, no separation between sections and procedures, redundant assignments, and on and on. This example is made up, but we've all seen these "features" in code. How can you maintain code like this?

Rather than try to give a long list of "standards" or rules, let me reduce it down to just a few very simple suggestions. There is, of course, no "correct" way to do this, but the following are, in my opinion only, good ways of formatting code.

  • Begin coding the following in column 1:
    • begin-program (-report), begin-procedure, begin-setup, begin-heading, begin-footing
    • Database column names in a SELECT block (this is required by SQR)
    • Compiler directives: #include, #ifdef, #define, etc.
    • Comments, as appropriate
  • No other elements other than the ones listed above should begin in column 1. Indent everything else using a consistent number of spaces. (I use two spaces.)
  • Indent one level under each of the begin- elements listed above and each if, while, evaluate, when, begin-select, begin-sql. The end should align with this keyword. Rule of thumb: Indent under when and under anything that has an associated end- keyword.
  • Indent within the SQL inside of a begin-select. This means that the SQL (FROM, WHERE) will be indented one level and the SQR code within the block will be indented an additional level.
  • Don't use tabs for indentation. Use spaces instead.
  • Use all upper case for the following:
    • SQL clauses
    • Database column names
    • #define constants
    • Literals, such as 'Y' and 'N'
  • Use lower or mixed case for all other elements.

Here are the reasons for these suggestions. First, $THISISREALLYHARDTOREAD. Code in mixed or lower case is easier to read in general. However, some databases are case sensitive, so I suggest coding SQL in all caps. This also helps to set the SQL off from the SQR code.

Indentation is very important for following the logic of a program. When you come to an if, you often want to find the else or end-if quickly. Without indentation, you need to scan every line...probably forgetting what it was you were looking for.

I place #define and other compiler directives in column 1 to highlight the fact that they are not part of the logic flow of the program. They are processed before the program even starts. Aligning them with the rest of the code would lead the reader to think that they are part of the logic flow.

Why do I suggest not using tabs? Different text editors handle them differently and/or allow different settings for the number of spaces in a tab. Therefore, you don't have any control over how the code will appear to other people if you use tabs. What looks aligned to you may very well end up as a random mess in someone else's editor. Use spaces to indent.

Note that begin-select would never be found in column 1 under the guidelines above. Many programmers assume that it must be there; in fact, only the names of the database columns (fields) that you are selecting must be in column 1.

It's also common practice to use hyphens in procedure names but to use underscores or mixed case in variable names. For example, we would have $variable_name or $VariableName.

So here's an example showing these suggestions in practice:

      !---------------------------------
      begin-procedure Procedure-Name
      !---------------------------------

        begin-select
      FIELD1
      FIELD2

            if &field1 = 'Y'
              if #SomeVariable = 1
                do Other-Proc-1
              end-if
            else
              do Other-Proc-2
            end-if

            do Update-Table

          FROM TABLE_NAME
          WHERE FIELD2 = 123
        end-select

      end-procedure


      !---------------------------------
      begin-procedure Update-Table
      !
      ! What this procedure does.
      !---------------------------------

        begin-sql
          UPDATE ...
          WHERE  ...
        end-sql

      end-procedure

Until next time...







 

  HOME  |  ABOUT US  |  PRODUCTS  |  SERVICES  |  TECH TALK  |  LINKS  |  SQR  |  CONTACT
© 2003-2010 SparkPath Technologies, Inc. & its licensors. All rights reserved. Trademarks used are property of their respective owners. | Terms of Use