The Leading Educational Resource for IT Professionals

Using Scope in Linear-Main Programs to Create More Stable Applications

by Victoria Mack August 19, 2016 0 Comments

Linear-main RPG programs eliminate the RPG logic cycle and add new levels of variable scoping to protect your code from bugs down the road.

 

While I am no expert in the RPG logic cycle, I have had to deal with it in older applications over the years. Most RPG developers have dealt with a logic cycle program at least once. I can honestly say I have never written a new logic cycle program, but I have seen others in the community doing it. This article is not intended to start a religious war about cycle programming. There are some who will never give it up. Instead, this article will demonstrate how to create a program without the logic cycle and concentrate on what I think is a very useful benefit to using linear-main procedures in program.

 

Variable Scoping

One of the biggest advantages of using subprocedures in RPG instead of subroutines is the ability to define variables, data structures, and even files locally within the subprocedure. Locally defined variables cannot be accessed or modified from outside of the subprocedure. This provides insulation from inadvertent effects from other parts of the program.

 

Ideally, a subprocedure will use only locally defined variables in its code. You can think of the subprocedure as a miniature program. It accepts and returns parameters and should be as self-contained as possible. However, a subprocedure can access and modify anything defined globally in the program; that is anything defined outside of a subprocedure. There are, of course, times when using globally defined elements is useful, but it should be the exception instead of the norm.

 

When writing a traditional RPG program, also known as a “cycle-main” program, your mainline code is coded at the global level. That means that any variables, data structures, or files needed in your mainline code must also be defined at a global level. This allows these elements to be open to manipulation from anywhere in the program. In very large programs, this allows unintended bugs to be introduced in the program from even very minor changes.

 

Our Example Program

To illustrate how global scoped variables can be dangerous, let’s look at a very simple RPG program.

 

       // global defines

       Dcl-S counter int(5);

 

       // mainline code

       For counter = 1 to 10;

         Dsply counter;

         My_Procedure();

       EndFor;

 

       *inLR = *On;

       Return;

 

       // subprocedures

       Dcl-Proc My_Procedure;

 

         // local defines

         Dcl-S counter int(5) ;

 

         For counter = 1 to 3;

           // Do Stuff

         EndFor;

 

       End-Proc;

 

As you can see, this program defines a counter variable that is used in the mainline of the code for a for loop. There is also a subprocedure called My_Procedure that has its own counter variable defined locally. This program will work just fine as coded. The program will output 1 through 10 on the screen as expected, because the counter variable in the subprocedure is scoped locally and is a completely different variable than the counter at the global level.

 

But what if I forgot to define counter in the subprocedure? What if I misspelled counter in the definition? Well, the program would compile without error because the field name exists globally. The program would also run, but I now have a bug. After displaying a 1, the program will get stuck in an infinite loop, outputting the number 5. Why? Because the for loop in the subprocedure will increment counter to 4 and exit. Then the mainline will increment counter to 5, display 5, and call the subprocedure. The subprocedure will loop around until counter equals 4 again, and the process repeats.

 

Take the code above and try it for yourself. If you comment out the local variable definition in My_Procedure, you will see what I mean. This is a simplified illustration of the way many bugs find their way into our code. Counters are just one example. I find most developers use a standard set of counter variables in their programs. Some use the name “counter.” Others like to use x, y, and z. It’s all just preference. It is easy to see from the example above how counters can get you in trouble, but counters aren’t the only types of variables that can be manipulated unintentionally. Any variable at the global level can be problematic, and identifying those types of bugs is not always as easy as spotting an infinite loop.

 

Creating a Linear-Main Program

So how can we protect our mainline code and variables from outside interference? The answer is simple. By using the MAIN keyword on the control spec, we can specify a subprocedure to be used as the mainline code for the program.

 

       Ctl-Opt Main(Main);

 

       // global defines

 

 

       Dcl-Proc Main;

         // local defines

         Dcl-S counter int(5);

 

         // mainline code

         For counter = 1 to 10;

           Dsply counter;

           My_Procedure();

         EndFor;

 

         Return;

       End-Proc;

 

       // subprocedures

       Dcl-Proc My_Procedure;

 

         // local defines

         Dcl-S counter int(5) ;

 

         For counter = 1 to 3;

           // Do Stuff

         EndFor;

 

       End-Proc;

 

Notice the Ctl-Opt keyword with the MAIN attribute set to Main. I always name my linear-main procedure Main for consistency. This is also the convention used in other languages, such as C/C++ and Java, so it should seem familiar to developers familiar with those languages.

 

Also note that I simply wrapped the declaration of counter and my mainline code with the declaration of my Main subprocedure. This defines my counter variable locally in my subprocedure. Now no matter what happens in My_Procedure, the counter variable cannot be affected. I can now be assured that my mainline code and its variables are isolated from any other part of the program unless explicitly passed into other subprocedures as a parameter.

 

If I do want a variable or data structure to be defined globally, I can still define it outside of the Main subprocedure, but I would do so only if I had a specific requirement for it. In general, I avoid globally defined elements if at all possible.

 

The last change you will notice is the removal of *inLR = *On. When you define a linear-main program, the RPG logic cycle is not included in the program. The last record indicator has no meaning.

 

This brings up an important note. Because the RPG cycle is not included in the program, there are some distinct differences with regard to globally defined elements. Globally defined files must be explicitly closed. A globally defined file will be opened automatically on the first call of the program, but it will not be closed automatically. It will also not be reopened automatically on subsequent calls. Globally defined variables and data structures will be initialized only on the first call of the program. They will not be reinitialized on subsequent calls as they would when an RPG cycle program ends with *inLR=*On.

 

Final Thoughts

As you can see, the setup of a linear-main program is actually quite simple. There are definitely advantages to doing so. Variable scoping is only one example. There are of course differences in programs when you eliminate the RPG cycle, and this can be a bit of an adjustment for long-time RPGers. Change is not a bad thing, and I encourage you to give linear-main programs a try. I write all of my new programs this way and have not missed the cycle at all.

 

It is important that we as developers strive to learn new things. What I’ve shown you is something that can be learned very easily and can have real benefits in your applications. Now get started!

 

About the author: Brian May

 




Victoria Mack
Victoria Mack

Author



Also in MC Press Articles

Bluemix: A Viable Option for Power Customers

by Victoria Mack August 19, 2016 0 Comments

Just what is Bluemix, and what could it mean for you? An interview with an IBMer reveals the answers.

steve pitcherWritten by Steve Pitcher

Last week, I sat down with Adam Gunther, Director of Cloud Developers Services at IBM, to talk about IBM Bluemix. I told Adam I wasn’t a developer up front, but I wanted him to explain just exactly how my small-to-medium-sized business with an investment in on-premises infrastructure could really take advantage of Bluemix. I wasn’t disappointed.

Continue Reading →

Midrange MQ in an Open-Source World

by Victoria Mack August 19, 2016 0 Comments

MQ on IBM i continues to adapt to the needs of modern IT environments.

andrew schofieldWritten by Andrew Schofield

IBM MQ has been a familiar part of the corporate IT landscape for over 20 years. It’s been through a few name changes, but the fundamental idea of using asynchronous messaging to decouple communication between applications is as important now as it has ever been. Of course, over such a long period of time, there have been huge changes—in particular, the way that developers work using the Internet and open-source, and the rise of cloud computing. Therefore, we at IBM are doing many things in MQ to make sure that existing systems remain relevant and able to interact with the latest tools and platforms.

Continue Reading →

SQL 101: Date-Related Functions, Part 3 - Extracting Information from Dates

by Victoria Mack August 19, 2016 0 Comments

This article continues the date-related functions discussion, introducing a few more simple but extremely useful SQL functions: DAYOFWEEK, WEEK, QUARTER, DAYOFYEAR, and MIDNIGHT_SECONDS. Do you have time for some date fun?

rafael victoria preiraWritten by Rafael Victória-Pereira

Let me start with a quick flashback: an RPG Academy TechTip published in October 2015, explaining how to create an RPG function to calculate the day of the week of a given date stirred things up quite a bit. Some readers complained this kind of function was totally unnecessary, because SQL is better equipped to do this type of thing and so on. My reply was that I’d get to a point in the SQL 101 series in which I’d cover the “SQL version” of that particular function, named Clc_DayOfWeek.

Continue Reading →