AS/400 RPG Best Practices
Introduction
IBM i (AS/400) systems often use RPG (Report Program Generator) for enterprise applications. Adopting best practices ensures maintainability, performance, and security.
1. Use Free-Format RPG
Modern RPG (RPGLE) supports free-format coding, making the code more readable and maintainable.
**Free DCL-S CustomerName CHAR(50); CustomerName = 'John Doe'; DSPLY CustomerName; *INLR = *ON;
2. Leverage SQL for Database Operations
Use embedded SQL for efficient database interactions instead of native record-level access.
**Free DCL-S CustName CHAR(50); EXEC SQL SELECT NAME INTO :CustName FROM CUSTOMERS WHERE ID = 1001; DSPLY CustName; *INLR = *ON;
3. Modularize Code with Procedures
Break complex programs into reusable procedures to improve maintainability.
**Free DCL-PR GetCustomerName CHAR(50) EXTNAME('CUSTLIB'); ID INT(10); END-PR; DCL-PI GetCustomerName CHAR(50); ID INT(10); END-PI; EXEC SQL SELECT NAME INTO :GetCustomerName FROM CUSTOMERS WHERE ID = :ID; RETURN GetCustomerName;
4. Use Display Files for User Interaction
Separate UI logic from business logic using DDS display files.
A R CUSTOMER A CUSTID 5 0B 5 2 A CUSTNAME 20 B 7 2 A ENTERKEY 1 0B 9 2 A K ENTERKEY
5. Optimize Performance
- Use appropriate indexes in DB2.
- Minimize I/O operations by retrieving only necessary data.
- Use commitment control for transactions.
6. Secure RPG Programs
Follow best security practices to protect sensitive data.
- Validate all user inputs.
- Use adopted authority instead of running programs as *ALLOBJ.
- Encrypt sensitive data before storage.
Conclusion
Following these RPG best practices ensures your applications are modern, maintainable, and secure.