ILERPG.com β€” IBM i Β· ILE RPG Β· AS400 Mastery
2 min readΒ· Admin

Beginner ILE RPG, Consolidated: One Program and a Best-Practices Checklist

Lessons 13 through 29 each introduced one part of the language. Lesson 30 puts them together in a single program of the kind a developer is likely to maintain in a production library, and then turns the series into a checklist for reviewing code.

The program

PRCORD prices every line of an order and stores the order total. It is a linear-main program whose entry parameters are an order number and a return code. Its prototype lives in a guarded copy member shared with every caller. Order lines are read through an externally described keyed file into a local qualified data structure, priced by two subprocedures with clear contracts, and updated. The order total is computed and stored with embedded SQL, checked through SQLSTATE. A MONITOR group converts unexpected errors into a return code instead of an operator message.

priceOrder('SO10045' : rc);

select;
  when rc = RC_OK;
    dsply 'Order priced';
  when rc = RC_NOT_FOUND;
    dsply 'No lines for that order';
  other;
    dsply 'Pricing failed - see job log';
endsl;

The checklist

  • Fully free-form source with **FREE; OPTION(*SRCSTMT : *NODEBUGIO).
  • Data types chosen deliberately; named constants instead of literals with business meaning; named indicators instead of numbered ones.
  • The narrowest scope for every definition; STATIC only with a written reason.
  • Qualified data structures and templates instead of parallel arrays; bounds checks against %ELEM.
  • Every file declared with the usage it needs; %EOF and %FOUND tested after every read and chain.
  • Program-described data validated before use; level checks never suppressed.
  • Inputs through CONST parameters, results through RETURN; prototypes in guarded copy members.
  • MONITOR around operations that can fail; SQLSTATE checked after every SQL statement.
  • Every object compiled with a debug view.

What comes next

The next phase of the course, Beginner to Intermediate ILE RPG, starts from this program: service programs and binding, SQL cursors, commitment control, and reading the fixed-format code that still runs much of the world's IBM i workload.

Get the lesson

Lesson 30, β€œBeginner ILE RPG Consolidation and Best Practices Review,” is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series β€” all 18 lessons β€” is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.