Even if the entire application is eventually rewritten in C/C++, I think that converting to free-form ILE RPG is a necessary first step. The converted code would be much easier to understand and modify. Also, I think it would be easier to change monolothic RPG programs into more modular function-based RPG code than to go straight to a modularized C/C++ application. Moving RPG code out of a program into a function is a relatively simple thing to do, and any problems discovered in testing can be blamed on the change to use a function. If the change also involved writing that function in C/C++, it would be much more difficult to determine why the testcase had failed.
That said, I don't think there is anything that can be done in RPG that can't be done in C/C++, but some things that are easy in RPG are more difficult in C/C++. For example, #pragma mapinc must be used to get the layout of the records for externally-described files, but that is practically automatic in RPG: it is the default behaviour for a free-form file deifnition, and it only needs one byte ('E' = Externally-described) in the fixed-form file file definition.
There are some things that almost seem like magic to people who are not familiar with RPG, such as the way all the fields in a file are automatically available as program variables, which are automatically set from the I/O buffer during a READ operation, and automatically copied into the I/O buffer during a WRITE or UPDATE operation.
For example, this program reads the records from an imaginary database file, puts up a screen showing the current values of the fields, and then updates the database record with the changes from the user.
Rewriting this program in C/C++ would require many more lines of code.
dcl-f dbfile usage(*update); // Database file opened for update
// All the record formats are available (DBFMT)
// All the field names are available as program variables
// (FIRSTNAME, LASTNAME, ADDRESS, CITY, COUNTRY)
dcl-f screens workstn; // Display file opened for input/output
// All the record formats are available (SCREEN, ...)
// All the field names are available as program variables
// (FULLNAME, ADDRESS, CITY, COUNTRY)
read dbfmt; // Read the first database record
dow not %eof; // Do while not end-of file
fullname = %trim(lastname) + ' ' + %trim(firstname);
exfmt screen; // Interact with the user (WRITE + READ)
update dbfmt; // Update the database record with the values
read dbfmt; // Read the next record
enddo;
*inlr = *on; // For a "cycle-main" procedure,
// this causes files to be closed, set things up so everthing
// gets re-initialized for the next call, and returns from
// the program
------------------------------
Barbara Morris
------------------------------
Original Message:
Sent: Fri December 06, 2024 06:07 AM
From: Ishtar Terra
Subject: In-depth understanding of C/C++ use cases
Hypotetical scenario
Historically, IBM i worked mainly with RPG. Let us imagine a company which has a lot of legacy RPG fixed-form code. The company is not willing to convert its codebase in RPG free-form. In fact, the company wants to ditch RPG (because its talent pool is shrinking), but still wants to keep IBM i as its primary system (because IBM i has many advantages, security on top).
Here's the idea: the company plans to rewrite their entire RPG codebase in C/C++, in order to find more young programmers, thus future-proofing their system.
Here are my questions:
- does a rewrite from legacy RPG to C/C++ makes sense? With make sense I mean if such a rewrite can offer advantages in contrast of migrating to RPG free-form code;
- is C/C++, as a programming language, capable of creating software with all of the functionalities of the same software written in RPG?
- Why companies do not adopt this approach?
- What are, in the real world, some use-cases of C/C++ in IBM i?
------------------------------
Ishtar Terra
------------------------------
#C/C++andFortran