Refactoring Lazy RPG Program code is the reason I am going grey

IBM i

Aug 15

Honestly, I despair of the RPG monkeys that knock up code that’s as bad as some of the things I’m working on currently. I found a great term to describe what I’ve been doing of late: refactoring

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a ‘refactoring’) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.

I’ve just spent a few hours taking some pre-y2k code and refactoring it to make it Y2K compliant! No. I’m not kidding. I know it’s 2008 but this code has been live for 8 years and consistently failing when sorting by date. The users were just living with the fact that if they pressed certain function keys the program would crash. Bless their cotton socks.

I’ve come up with a fairly standard refactoring process that I follow for most RPG code.

CVTRPGSRC – run the old RPG through IBM’s conversion to RPGLE layout. At the very least you get some nice IF/DO/END statements if you are lucky enough to be touching code that is vaguely structured.

Dive into code and add comments to the program from start to finish. This is a nice way to get the program logic into your head.

Get Editing – I usually start with simple indicator loops (i.e.: changing nXX indicators to IF/END loops) and end up with changing any GOTO/CABEQ to more structured layouts. Next is renaming work fields to make more sense, chaining with Keys rather than reading entire files comparing field values (yes honestly!)… putting standard *INZSR and *PSSR routines into place… and lots of other little tweaks….

UPGRPGSRC – I use this PROJEX4i utility to parse the source code andupgrade to freeform, optionally doing things like coloring the comments blue and switching them to lower case makes the code more readable. Nested loop numbering is useful as well.

Now it’s back into EDIT mode….

It’s quite sad that I enjoy doing this. It’s fairly simple, modular and relaxing in a weird quirky geekish way.

I wonder if I could write a REFSRCMBR – Refactor Source Member? 😉

Follow

About the Author

IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and in the words of the most interesting man in the world: Stay thirsty my friend.