How to write Better and Efficient PeopleCode (for Beginners) - Part I

As PeopleSoft developers sometime following these simple guidelines will not only improve performance but also improve readability of code you have written and help to troubleshoot easily if you encounter any problems.

Declare variables before you use them

Unlike other programming languages (such as Java, C#) PeopleCode does not enforce you to declare variables before use them. If you declare a variable without a datatype (e.g integer or string) that variable is assigned the type Any. Which means the data type is indeterminate, enabling PeopleTools to determine the appropriate type of value based on context. However, if you use this feature, you lose type-checking at compile time, which can lead to problems at runtime. When you save the PeopleCode or validate the PeopleCode look for auto declared messages in Valide pane at the bottom of the app designer.

Declare variable types specifically.

PeopleCode has following conventional data types
Any, Boolean, Date ,DateTime ,Float ,Integer, Number, String and Time
Note: The Float and Integer data types should be used instead of Number only when a performance analysis indicates that the increased speed is useful and an application analysis indicates that the different representations will not affect the results of the computations.
It is always a good programming practice to declare your variable to a specific data type so that it will reduce code confusions and improve application performance.

PeopleCode Objects and API Objects must be declared to the corresponding data type to instantiate objects from that class.

Reference and Value Parameters

Unlike other programming languages, in PeopleCode function call parameters are passed by reference. Which means when you have used parameters, you have passed a value into a variable used by the function. Any changes made to this variable in the function will change the original value of the parameter specified in the function call.
Have a look on this example;
consider a function that doubles and displays the value of a passed parameter,
function ShowDouble(&val as integer)
&val = &val * 2;

/*Here, the parameter, &myVal, is doubled in this function. If you call it like this*/

Local integer &myVal = 5;
ShowDouble(&myVal); /*&myVal = 10*/

Put Break statements in your Evaluate statements

In an Evaluate statement, the When clauses continue to be evaluated until an End-evaluate or a Break statement is encountered.
If you have an Evaluate statement with a number of When clauses, and you only expect one of them to match, put a Break statement following the likely clause. Otherwise, all the subsequent When clauses are evaluated. Your program is still correct, but it is inefficient at runtime, particularly if you have a large number of When clauses, and the Evaluate statement is in a loop.

Ayesha Wee

    Blogger Comment
    Facebook Comment


Post a Comment