Peoplecode Coding Standards

Peoplecode Coding Standards

I. Declaring Local Variables 

Background

Except for Application Package Peoplecode, the Peoplecode compiler/editor does not require that local variables used by programs are declared. Undeclared variables are automatically allocated the type of Any.

Coding Standard

When writing Peoplecode, declare ALL local variables that will be used.

Benefits

  1. Declaring the specific type of variable needed improves performance, in contrast with the interpreter always assuming a type of Any. Even if the variable to be declared requires being an Any type, declare it nonetheless (see 4th item below).
  2. Compile-time validation.
    • When variables are declared, the Peoplecode editor can check the compatibility of variables used in an operation, or passed as function arguments.
    • For variables pointing to objects, the names of methods and properties can be validated at save time.
  3. Convenience. For built-in buffer classes (Rowset, Row, and Record), shorthand notation is only allowed for declared variables.
  4. For programming languages that allow dynamic allocation of variables, it is a common programming error to misspell variable names. However, on the Peoplecode editor, when all variables are consciously declared, it is easy to check for misspelled variable names (see the following illustration). Thus, with this standard in place, this type of coding error would be eliminated.

How long did it take you to find them 2 misspelled variables?

The code could be modified to enforce the rule that all variables are consciously declared; resulting in the following (changes are highlighted): See 2nd image above.

Now, when this Peoplecode is saved, or when the Validate Syntax action is invoked. The following messages would be displayed in the Output window See 3rd Image above

Double-clicking these messages would bring you to the offending line in the code.

NOTE: As shown in the example above, it is NOT necessary to always declare the variable at the top of the program. Declaring them on the first time they are used is sufficient to achieve the said benefits.

-Ganesh A.M

 

 

0 comments: