Skip to content

Documenting RPG code with AI: what works and what you must verify

B2Bnetwork team · · 6 min

IBM i, RPG and mainframe

In brief

A language model is good at turning a single RPG program into a plain-language description, summarizing subroutines, proposing a field dictionary and describing the data flow. It gets things wrong where the meaning depends on something outside the program: indicators set elsewhere, MOVE operations on fields of different lengths, file overrides in CL and data in the LDA. So the facts about dependencies come from system commands (DSPPGMREF, DSPDBR), AI writes the draft, and an engineer verifies it with a test before the description goes into the documentation.

What AI does well and what it does less well

A language model reads RPG faster than a person and does not get tired after three hundred programs. The quality of the result, though, depends on the kind of task.

TaskHow it turns outHow to verify
A few sentences on what a program doesgood for programs up to a few hundred linesa read-through by someone who knows the process
Summaries of subroutines and proceduresgoodspot checks against the code
A field dictionary with descriptionsgood when fields have descriptions in DDS or SQLcomparison with the descriptions in the database
A list of files and callsfair, the model may miss somethingDSPPGMREF as the source of truth
Business rules with conditionsfair, it loses indicators and dependenciesa test with data that demonstrates the rule
The flow of a whole process across many programspoor without contexta map from DSPPGMREF, the scheduler and interviews

The conclusion is simple: AI speeds up writing but does not replace facts from the system. It helps most when combined with the method described in Documenting a legacy system when its authors are gone.

Where AI gets old RPG wrong

Fixed-form code carries meaning in columns, indicators and operation behavior that the names do not reveal. An example:

     C     NRKL          CHAIN     KLIENCI                            90
     C     *IN90         IFEQ      *OFF
     C     SALDO         SUB       LIMIT         WOLNE
     C  N55              MOVE      'B'           STATUS
     C                   ENDIF

A typical model description reads: "if the customer exists, the program calculates the available limit and sets status B". (The field names are Polish: customer number, customers file, balance, limit, available.) Every part of that sentence needs checking:

  • Indicator 90 turns on when CHAIN does not find the record, so the condition "*IN90 = *OFF" means "found". Here the model is usually right.
  • Indicator 55 conditions the MOVE, but it is set somewhere else: in another subroutine, in a display file or in the calling program. Without that knowledge, the rule "sets status B" is incomplete.
  • MOVE moves the value to the right-hand side of the field. If STATUS is three characters long, MOVE 'B' changes only the last character and leaves the first two. Models often describe this as a plain assignment.
  • Externally described fields. SALDO and LIMIT may come from the KLIENCI file, from another file or from a data structure. Their type and length are in the file description, not in the program.

Other common traps:

  1. File overrides in CL. An OVRDBF in the controlling program can make the program read a different file or library than the RPG code suggests.
  2. The LDA and data areas. Parameters passed through the local data area do not show up in the parameter list.
  3. The RPG cycle. Programs with a primary file run in a loop that is not visible in the code.
  4. Overlapping data structures. Changing one field changes another, which is easy to miss.

Errors become more frequent with program length and the number of members pulled in through /COPY. Where wrong answers from models come from in general is covered in Language model hallucinations.

The process step by step

  1. Export the sources from source physical files to the IFS with CPYTOSTMF and keep them in a git repository.
  2. Collect facts from the system. DSPPGMREF lists the files and programs each program uses, and DSPDBR the dependencies between files. These lists are the source of truth for the dependency map.
  3. Give the model context. Besides the program code: file descriptions (DDS or DDL), /COPY members, the CL program that runs it, and the DSPPGMREF output.
  4. Draft the description to a fixed template. The program's purpose, inputs and outputs, files with access type, business rules with line numbers, open questions.
  5. Engineer review. Every rule in the description points to a line of code. Rules with doubts go onto a list of questions for users.
  6. A test as proof. For the most important rules, write a test with data that shows the rule at work. The test stays in the repository and keeps the description from drifting away from the code.

The open questions are as valuable as the description. A list saying "we do not know who sets indicator 55" shows where knowledge exists only in people's heads.

Data, code and security

Application code is company property and often contains rules competitors should not see, and sometimes data too: technical account numbers, server addresses, passwords in CL programs. Before sending code to a model:

  • choose where processing happens: a model on your own server, in a data center in Poland, or a service whose contract rules out training on your data;
  • search the sources for passwords and addresses and remove them from the code, regardless of AI;
  • do not send production data to describe a program, because file descriptions and code are enough.

Format and location of the documentation

Documentation lives longest next to the code: a Markdown file in the repository, in the same directory as the program source, with a dependency map generated by a script from DSPPGMREF. Whenever a program changes, the code review covers the description too. How to move the sources to git is covered in Git and CI/CD for IBM i, and how to switch to a more readable code format along the way in Converting RPG to free-form.

Describing the application, mapping processes and planning next steps fall under Analysis and architecture. In that work B2Bnetwork engineers combine facts from the system, interviews with users and drafts from a language model, and check every rule in the code.

Questions and answers

Can AI write the documentation for a whole RPG application on its own?

It can write a draft, but not documentation you can rely on. The model does not see indicators set in other programs, file overrides in CL or data in the LDA. Without an engineer's review and tests, the description will be wrong in the places that matter most: the business rules.

How much time does AI save when documenting RPG?

The most on the first draft: program descriptions, subroutine summaries and field dictionaries come together faster than when written by hand. Review, filling in dependencies and testing rules still take an engineer's time. The gain depends on the size of the programs and how much context the model gets.

Is it safe to send RPG code to a cloud model?

It depends on the contract and the code. Before sending, remove passwords and addresses, check that the provider does not train on your data, and check where it processes data. When the code contains especially sensitive rules, it is better to use a model on your own server or in a data center in Poland.

Which programs should we document with AI first?

Start with the programs that change most often or cause incidents, and with those nobody on the team knows. Change history, tickets and the objects' last-used dates give you the list. Programs nobody has run for years are worth marking as candidates for removal first.

What it looks like in a project

Developing a system on IBM i or mainframe? Talk to an RPG engineer.

Let's talk

Read more: IBM i, RPG and mainframe

All in IBM i, RPG and mainframe →