Pages

Sunday, November 22, 2015

Advantage of Using Sub-Procedures over Sub-routines in RPGLE

Need for Sub-Procedures or Sub-Routines:

The main advantage of using subroutines is intellectual manageability. That is, I can think of a program as a group of related small tasks, all of which are simple enough for me to understand, rather than as one gargantuan task that is more than I can handle. This gives ease in modifying/bug fixing of a program.

Debugging seems to be easier because I can often determine which subroutine most likely contains an error. Finding a logic error in a program of sub-procedures/sub-routines is easier when compared against the program which has complete logic in single routine.

Sub-Procedures/Sub-Routines also promotes the re-usability of code.

Limitations of Sub-Routines:

1.     Sub-Routines use global variables only.

That is, any variable or constant or indicator that I use in a subroutine may be used anywhere else in the program--in the main calculations, in any subroutine, in output specs. This can lead to undesirable side effects. Changing the value of a variable in one part of the program causes something to go wrong in another part of a program.

So, copying the code of sub-routine to different program would need changes to be done in view of variables.

2.     Inability to use Parameters.

If we have a code that performs specific calculation based on input parameters, we would need to initialize the variables before calling the program.

Advantages of Sub-Procedures:


1.     Sub-Procedures allow you to define local variables.

Local variables would be allowed only inside sub-procedure. So, there is no risk by using the variables with same name in other sub-procedures or main line. These variables cannot be referenced outside of the program.

2.     Ability to use parameters.

Parameters make sub-procedures more portable by providing internal names for required data. It makes program maintenance easy when the procedure is being called from multiple routines.

3.     Returning a Value

Sub-procedure has the ability to return a value. It can be used as how BIF are being used on operations like EVAL, IF etc.

4.     Ability to use Sub-Procedure across application.

If we have Sub-Procedure that will be useful across application, It can be attached to Service Program and made available to access across application.

No comments:

Post a Comment

How to Iterate Over a List and Get Indexes with 'enumerate' Function in Python

'enumerate' function There are different ways of accessing the data from a list like using range and then index or accessing the ele...