Don’t Just Modernize AS400 RPG - Refactor It!

If you’re staring at a wall of crusty RPG III or RPG/400 code and thinking, “Let’s just convert this to free-format RPGLE,” hold up. Modernization isn’t just about changing the syntax, it’s about rethinking the logic, streamlining the structure, and embracing modern opcodes that make your code cleaner, faster, and easier to maintain.

The Old Way: RPG/400 Fixed Format

Here’s a classic loop from the 1980s:

C      Z-ADD     0           TOTAL      10 0
C      DO        100         I
C      Z-ADD     I           VALUE      10 0
C      ADD       VALUE       TOTAL
C      ENDDO
C      SETON                                LR

This works. But it’s verbose, clunky, and full of unnecessary steps.

Why Z-ADD when you can just declare and assign? Much as I love the Z-ADD opcode, the modern EVAL just makes more sense.

The Refactored Way: RPGLE Free Format with Modern Opcodes

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-s total int(10);

for i = 1 to 100;
   total += i;
endfor;

*inlr = *on;

Boom. No Z-ADD. No temporary variables. Just clean, readable logic using for, +=, and dcl-s.

Don’t Just Convert, Think and Improve (Refactor)!

Here’s another example. Old-style conditional logic:

C      READ     FILE1
C      IF       FIELD1 = 'X'
C      EXSR     SUBRX
C      ENDIF
C      IF       FIELD1 = 'Y'
C      EXSR     SUBRY
C      ENDIF
C      IF       FIELD1 = 'Z'
C      EXSR     SUBRZ
C      ENDIF

Refactored might look something like this:

dcl-s fieldValue char(1);

read FILE1;
fieldValue = FIELD1;

select;
when fieldValue = 'X';
  exsr SubroutineX;
when fieldValue = 'Y';
  exsr SubroutineY;
when fieldValue = 'Z';
  exsr SubroutineZ;
endselect;

Better yet, replace EXSR with a proper subprocedure in a service program. That’s real modernization.

Modern Opcodes You Should Be Using

for, dow, dou → Replace DO loops

select, when, other → Replace nested IF/ELSE

dcl-s, dcl-pi, dcl-pr → Replace I and C specs

%trim, %subst, %scan, %check → Replace manual string manipulation

%error, %found, %eof → Replace status indicators

Clean Code Is Maintainable Code

Modern RPG isn’t just about free-format, it’s about clarity, modularity, and efficiency. Refactoring lets you:

  •  Remove legacy cruft
  • Use built-in functions
  • Break logic into reusable procedures
  • Write code that future-you (or your junior devs) will actually understand

Final Thoughts

Modernizing RPG code is like renovating a house. You don’t just slap on a new coat of paint you check the wiring, knock down walls, and maybe add a hot tub. So don’t just convert your RPG/400 code to free-format RPGLE. Refactor it. Rethink it. Rebuild it.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>