|
Let Your Hair Down With Free-Formed C-Specs
by Kevin Vandever
RPG has undergone many transformations over the years. What started out as a cryptic, report-writing
programming language has grown into a full-fledged business computing language. Other languages have
come and gone, and many so-called experts claimed that RPG would be the next language to die. But
it hasn't, mostly because IBM has done a great job modifying the language to keep it current with the
technology around it.
The V5R1 implementation of OS/400 is no exception. RPG has, again, been made a stronger programming
language that is not only ready to meet today's OS/400 challenges, but also those that extend beyond
OS/400. New built-in functions, data types, and prototyping keywords have allowed RPG to better
communicate with Java. New op codes have blazed the trail toward more flexible and robust error trapping,
and calculation specification enhancements have opened the door to more readable, standard, and less-
restrictive coding practices. This article will address the free-formed C-specifications.
RPG in Java's Clothing
Take a look at the following code:
/Free
// Create String objects from the alphanumeric constants
String1 = newString(alpha1);
String2 = newString(alpha2);
// Create BigDecimal Objects from the String objects. We
// could create BigDecimal directly from a float or bigInt
// and save the String object creation step, but Sun
// recommends using Strings for data accuracy.
BigD1 = String2BigD(string1);
BigD2 = String2BigD(string2);
// Add the two BigDecimal objects. See how there are two
// parameters even though we only defined one. The first
// parm is the instance parm and the second parm is the
// one that we defined in the prototype.
Sum = add(BigD1:BigD2);
// Now convert the BigDecimal sum back to a string; then
// convert the String into alpha data for us to view.
StringSum = BigD2String(Sum);
DisplaySum = getBytes(StringSum);
// display the alpha version of the sum and end the program.
Dsply DisplaySum;
*InLr = *On;
/End-Free
Recognize it? It's the RPG code from my article
"Prototyping and Calling Java
Methods from RPG". At first glance, it looks like Java, with its semicolons to end statements and its
slashes (//) to signify comments. But, no, this isn't my RPG code converted to Java; it is the actual working
RPG code. That's right, no C in position 6, no column rules, and, yes, semicolons to end source statements
and slashes to signify comments. With the release of V5R1, we RPG programmers can now code free-
formed C-specifications.
To tell the compiler that you want to code freely, you place /Free in positions seven through 11 and start
coding. To end free-formed coding, you simply place /End-Free in positions seven through 15. In between,
you code as you normally would, except that you don't have to worry about a C in position six, nor do you
have any column constraints.
Another advantage is that you can eliminate op codes in a couple of cases. If you look at the code above,
you will notice that there is no EVAL operation to set on LR. With free-formed calculations, you can
remove the EVAL op code and just type the assignment statement, as you would in most other languages.
The other op code you can eliminate is CALLP. In my example above, I didn't use CALLPs, because I am
using my function calls as expressions. But even if I had used a CALLP in a traditional C-spec, I could
remove it when coding the same statement inside free-formed specs.
Another change from traditional C-specs is that you place factor 1 data after the operation when
coding free-form. Also--and this is a biggie--you can indent your statements to make them more readable.
This is great for those who still like to nest IF statements until the cows come home (hint: use a SELECT),
but it is also useful for grouping code in such a manner that it is easier to follow and maintain. The
following is a little code snippet that shows free-formed techniques that differ a little from their traditional
counterparts:
/Free
Field1 = 3; // Assignment statement with no EVAL
Field2 = 1; // Another assignment statement with no EVAL
// call the add subprocedure to add two fields together
Sum = add(field1:field2);
// call a subprocedure without using CALLP
Accumulate(sum);
// Chain to a file byplacing the op code first
// then the key to the file
// followed by the file
Chain(n) MyKey MyFile
If Not %Found;
ExSr Error;
ExSr Report;
Return;
Else;
Process(Data);
EndIf;
/End-Free
See how cool that is?
A Few Rules
There are a couple of other things to know about free-formed calculations:
You can mix them in with traditional calc specs, as long as you code the /free and /end-free correctly.
This includes code brought into the program from the /copy directive.
You cannot use resulting indicators in free-formed format (Hooray!); you must use the appropriate
built-in functions to determine file and record retrieval status.
If you have a long expression, you can continue on the next line by simply typing on the next line and
not placing a semicolon at end of the first line. This is allowed because, with fee-formed calculation
specs, all white space is ignored. The following is an example:
// long calculation
Sum = Number1 + Number2 + Number3 + Number4
+ Number5 + Number6;
You cannot define fields inside a free-formed block. You must use D-specs to define your variables.
This is actually a good thing, since you should never define fields in a C-spec.
As with Java, you must end a statement with a semicolon.
Finally, comments are identified with a double slash (//), as opposed to an asterisk (*), in column 7. In
addition, comments can be placed on a separate line, or on the same line, after the semicolon.
Free at Last, Free at Last
Some of you may have trouble getting used to free-formed calculation specifications. I understand. It is not
natural for us long-time RPG programmers. However, if you have experience with another language,
especially Java, this type of coding will come easy, if not naturally, to you. Free-formed calculations specs
allow you to group and nest your code more effectively, which leads to easier maintenance and readability.
It also brings the RPG language coding structure closer to that of other languages, which may ease cross-
training and help IT become more productive. There is one more advantage: Bring your newly coded free-
formed C-specs to the next code review meeting, and watch those who may not have heard of them before
jump right out of their seats.
Kevin Vandever is a lead IT engineer at Boise Cascade Office Products in Itasca, Illinois. He is also
co-editor of Midrange Programmer, OS/400 Edition. Kevin can be reached at
kvandever@itjungle.com.
|