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!


 




Victoria Mack
Victoria Mack

Author



Also in MC Press Articles

Customer (Citizen) Identity and Access Management

by Graham Williamson June 13, 2017 0 Comments

As a major trend in the IDM sector, consumerization has become easier and exponentially more important. Digital transformation will literally put a significant segment of the SME market out of business and propel a significant number of SMEs to new levels of prosperity.

Continue Reading →

Federated Authentication – there is no Plan B

by Graham Williamson June 05, 2017 0 Comments

Federated authentication is essential for businesses. It's the only way to effectively manage external access to business systems and it's absolutely necessary in order to manage authentication to SaaS apps. if you don't want to expose your identity records to potential compromise.

Continue Reading →

Access Control – RBAC & ABAC

by Graham Williamson May 04, 2017 0 Comments

Access Control is the core of the identity and access management task. Once we have correctly provisioned user data into the enterprise’s identity service we need to leverage it for access control. The vast majority of organizations use role-based access control, but increasingly, access control based on attributes is gaining traction.

Continue Reading →