Subprocedures and subroutines are both mechanisms to segregate logic, but subprocedures are preferred for three reasons:
- their local files and variables make maintenance faster and cleaner. They allow you to create variables without worrying about name clashes,
and to change fields without worrying about negatively impacting other parts of the program.
- their local files and variables make code reuse easy.
- they can be called with parameters as functions, yielding clearer more readable code.
Noncompliant code example
D FirstName S 20A
D Initial S 1A
D LastName S 20A
D FullName S 43A
/free
FirstName = 'John';
Initial = 'A';
LastName = 'Smith';
EXSR SPFullName;
DSPLY FullName;
...
begsr SPFullName;
FullName = FirstName + ' ' + Initial + ' ' + LastName;
endsr;
/end-free
Compliant solution
/free
DSPLY FullName('John':'A':'Smith');
...
/end-free
P FullName B
D FullName PI 43A
D FirstName 20A Const
D Initial 1A Const
D LastName 20A Const
/free
return FirstName + ' ' + Initial + ' ' + LastName;
/end-Free
P E