I was reading through some forum threads and saw an interesting question. Let me summarize:
How do I convert from a number to a string so that I get the integer portion of the number? I have 1234 in a number, but when I put it in a string field, I get 1234.00.
This is a great question! There are many PeopleCode "casting" functions, such as Number and String. Reading through the thread, I saw some interesting answers. One of them that I liked used Java to do the conversion. What I liked about this idea was the out-of-the-box thinking. But is it necessary? Can you do this with PeopleCode? The answer is YES using the functions NumberToString
and NumberToDisplayString
. Is there anything wrong with using Java instead? No. I love PeopleCode's ability to leverage the JRE. But there is a cost in context switching, JRE overhead, etc.
Keeping with the number and string scenario, do you really need to "cast" a number to a string? Consider the following code listing:
Local number &anInteger = 1234; MessageBox(0,"",0,0, &anInteger);
This code would fail because the fifth parameter to MessageBox
must be a string. Therefore, it seems we need casting functions. Or do we? Consider the following alternative (notice the string concatenation in the MessageBox function):
Local number &anInteger = 1234; MessageBox(0,"",0,0, "" | &anInteger);
When we concatenate a number to a string, PeopleCode applies an implicit cast. Interestingly, it only applies an implicit cast if we trick it with a zero-length string. As a side note, the above implicit cast returns the integer portion 1234 without decimals because 1234 has no decimals (which was the original request).
What about conditional logic? Here is one of my favorite examples:
If(PER_ORG_ASGN.POI_TYPE = "00002") Then JOB.COMPRATE.Visible = False; Else JOB.COMPRATE.Visible = True; End-If;
The above conditional code could be rewritten as:
JOB.COMPRATE.Visible = (PER_ORG_ASGN.POI_TYPE = "00002");
With an understanding of the PeopleCode language, we have reduced five lines of code into a single line. This might be considered the Spartan Programming approach. While I love the concept, Spartan Programming claims it "is not directly concerned with readability." Therefore, I prefer a slightly modified, possibly more human-readable alternative:
Local boolean &isVisible = (PER_ORG_ASGN.POI_TYPE = "00002"); JOB.COMPRATE.Visible = &isVisible;
Here is another piece of code I found several years ago:
Local Array of String &arr; ... REM ** delete all elements from an array; For &i = 1 to &arr.Len &arr.pop(); End-For;
If you look through PeopleBooks, you will see the Array has no clear, empty, deleteAll, or any other method for emptying all elements from an array. So I can see why the author wrote the code above. But the reason the Array object has no method for emptying an array is because it doesn't need one. The Array length is writable. The easiest way to empty an array is:
&arr.Len = 0;
How well do you know the PeopleCode language? Time for a refresher? Check out our on-demand PeopleCode course and advanced PeopleCode Application Classes courses and experience a new side of PeopleCode!
No comments:
Post a Comment