I was recording a screencast about modernizing some old RPG400 to ILE RPG this morning and had to knock up this little code example showing the difference between legacy RPG400 code and modern RPG ILE code.
Note, this is not an example of good code (it’s crappy old RPG code) but it’s just an example of the difference layouts and syntax of the three main version of RPG language – RPG2 the ancient logic cycle based stuff, RPG3/400 the old column-based stuff and RPG4 the newer/current free format stuff.
RPG Old and New
This code snippet, is an example that shows the same code, and logic in both old and new styles:
C DOU %EOF(FILENAME) C READ(E) FILENAME C IF %ERROR C EVAL MSG = '* SOMETHING DREADFUL HAPPENED' C LEAVE C END C IF %EOF C EVAL MSG = '* End of File' C LEAVE C ENDIF C KEYFLD CHAIN OTHERFILE C IF %FOUND(OTHERFILE) C EVAL FILEFIELD = OTHERFIELD C UPDATE OTHERFILE C ENDIF C ENDDO
dou %eof(Filename); read(e) Filename; if %error; msg = '* something dreadful happened'; leave; endif; if %eof; msg = '* End of File'; leave; endif; chain keyfld OtherFile; if %found(otherfile); filefield = otherfield; update otherfile; endif; enddo;
Note: I would further update this code to put the logic inside an if|elseif|else loop. It’s functionally the same but purely a personal style I prefer. I used to use select|when groups (which do the same thing) but tend to use if|elseif|else nowadays. No particular reason they both work the same way:
dou %eof(Filename); read(e) Filename; if %error; msg = '* something dreadful happened'; leave; elseif %eof; msg = '* End of File'; leave; else; chain keyfld OtherFile; if %found(otherfile); filefield = otherfield; update otherfile; endif; endif; enddo;
Guess which style I prefer 🙂