• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Aliases — Underused and Unappreciated

    October 3, 2022 Ted Holt

    One of the first things I learned about programming in the RPG II language was that field and variable names had to be six characters or less and they did not have to be pronounceable. I accepted this without question, as I was new to computers and figured that everything that had to do with computers was arcane and other-worldly. It wasn’t until I began work toward my computer science degree and was privileged to learn Pascal that I came to appreciate the value of longer identifier names, and of clarity of source code in general.

    You don’t have to have a computer science degree to use long field names in modern RPG. Even if your database files — physical and logical — have field names of six characters or less, you don’t have to use those short names ever again in RPG programs. It is my purpose in this article to show you how to use longer, more legible field names instead.

    Here’s some DDS for a physical file that (we’ll pretend) was first created in antiquity on an AS/400 from a file ported from a S/36.

    A                                      UNIQUE                              
    A          R INVOICE                                                       
    A            IVINUM         7  0       TEXT('INVOICE NUMBER')              
    A            IVDATE         7  0       TEXT('INVOICE DATE')                
    A            IVCOMP         3  0       TEXT('COMPANY NUMBER')              
    A            IVCUS#         5  0       TEXT('CUSTOMER ACCOUNT NUMBER')     
    A            IVDUDT         7  0       TEXT('DUE DATE')                    
    A          K IVINUM                                                        
    

    Notice that the first two characters of each field name are IV. This antiquated practice was common in bygone days. Each physical file had its own two-character variable name prefix so that fields from different files could not overlay each other in memory. There is no need to distinguish fields in this manner these days, but the custom endures.

    DDS limits field name length to 10 characters, which is not too bad. If you reserve the first two characters of each field name to identify the file, you have eight distinguishing characters in the field names. That’s still not too bad.  The compiler, of course, doesn’t care how long or short the field names are. But I care. I care because I have to read the code, and CMCRLT and CMCLRT look a lot alike.

    In Code Complete, Steve McConnell mentions a study of the relationship between length of identifiers and ease of debugging in COBOL programs. The study reported that debugging effort was less when identifiers tended to have names from 10 to 16 characters long. Programs with 8- to 20-character names were almost as easy to debug.

    Fortunately, it is easy to have longer field names in RPG programs. First, add the ALIAS keyword to the DDS.

    A                                      UNIQUE                              
    A          R INVOICE                                                       
    A            IVINUM         7  0       TEXT('INVOICE NUMBER')              
    A                                      ALIAS(INVOICE_NUMBER)               
    A            IVDATE         7  0       TEXT('INVOICE DATE')                
    A                                      ALIAS(INVOICE_DATE)                 
    A            IVCOMP         3  0       TEXT('COMPANY NUMBER')              
    A                                      ALIAS(COMPANY_NUMBER)               
    A            IVCUS#         5  0       TEXT('CUSTOMER ACCOUNT NUMBER')     
    A                                      ALIAS(ACCOUNT_NUMBER)               
    A            IVDUDT         7  0       TEXT('DUE DATE')                    
    A                                      ALIAS(DUE_DATE)                     
    A          K IVINUM                                                        
    

    An alias name has to be 30 characters or less for physical files defined by DDS. CREATE TABLE allows longer field names, but I’ve never needed them. Thirty characters is infinity for my purposes.

    Each field now has two names: a system name (a short one), and an alias (a long one).

    The nice thing about adding aliases to DDS-defined physical files is that there is no level check. You can add aliases to an existing file without having to recompile the programs that use the file. Happy day!

    Once you’ve added the aliases, your RPG programs can be free from the short names of 10 or fewer characters. I say “can be” because you have to do a little work. And I really do mean “a little.” You have to add the ALIAS keyword in two places: file definitions and externally-described data structures. Let’s look at a program that uses aliases.

    But first, another physical file, the customer master file. It also has aliases:

    A                                      UNIQUE
    A          R CUSTOMER
    A            CUCOMP         3  0       TEXT('COMPANY NUMBER')
    A                                      ALIAS(COMPANY_NUMBER)
    A            CUCUS#         5  0       TEXT('CUSTOMER ACCOUNT NUMBER')
    A                                      ALIAS(ACCOUNT_NUMBER)
    A            CUCNAM        20          TEXT('CUSTOMER NAME')
    A                                      ALIAS(NAME)
    A            CUCCLS         1          TEXT('CUSTOMER CLASS')
    A                                      ALIAS(CLASS)
    A            CUCRLM         7  2       TEXT('CREDIT LIMIT')
    A                                      ALIAS(CREDIT_LIMIT)
    A          K CUCOMP
    A          K CUCUS#
    

    Here’s the program. You don’t have to use free-form RPG when using aliases. I did this example in free form because that’s the way I write my code these days.

    dcl-f  InvHdrPF  disk  keyed  alias;
    dcl-f  CustPF    disk  keyed  alias;
    
    dow *on;
       read InvHdrPF;
       if %eof();
          leave;
       endif;
       chain (Company_Number: Account_Number) CustPF;
       if not %found();
          clear Class;
          clear Credit_Limit;
       endif;
       // do something with the data
    enddo;
    return;
    

    Notice the ALIAS keyword in the two DCL-F statements. Notice also that the CHAIN refers to aliases from the invoice header, and the two CLEAR operations refer to aliases from the customer master file.

    And that’s all there is to it.

    You can also use aliases in externally described data structures.

    dcl-ds  Invoice extname('INVHDRPF') qualified alias inz end-ds;
    
    clear Invoice.Due_date;
    

    Notice that the clear refers to the alias, Due_date, not the short name IVDUDT.

    Notice also that aliases don’t have to be longer than 10 characters.

    One last point and I’m done.  The ALIAS keyword is an RPG feature.  CL cannot reference the aliases. For instance, to copy all company-one records to a temporary file, you would use a command like this one:

    CPYF       FROMFILE(CUSTPF) TOFILE(QTEMP/CUST001) +
                 MBROPT(*REPLACE) CRTFILE(*YES) +      
                 INCREL((*IF CUCOMP *EQ 1))            
    
    

    When possible, I use SQL for input/output. SQL understands the system names and the aliases and doesn’t care which ones you use. I always use the aliases, of course. When I have to use native I/O, I qualify the files and use the aliases. Life is too short to fight with illegible source code.

    RELATED STORIES

    Code Complete

    ALIAS (Alternative Name) keyword for physical and logical files

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: CL, I/O, IBM i, RPG, SQL

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    We Need Some Insight From You Software Supply Chain Attacks Are A Growing Threat

    2 thoughts on “Guru: Aliases — Underused and Unappreciated”

    • Glenn Gunderman says:
      October 3, 2022 at 9:32 am

      This is useful for those afraid of recreating their tables using DDL.
      To be clear, the ALIAS keyword in DDS is not an RPG feature. While CL can’t take advantage of the alias, it’s in the database and can be used in SQL. e.g. select * from custpf where company_number = 1.

      Reply
    • ema tissani says:
      October 3, 2022 at 11:41 am

      Unfortunately many part of the OS and tools still reports or uses only the short field name (system name).
      Anyway, also with SQL CREATE TABLE you can specify short and long field names… and it highly suggested, because the system generate pretty ugly short ones if left alone.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 32 Issue: 65

This Issue Sponsored By

  • ProData
  • WorksRight Software
  • ARCAD Software
  • New Generation Software
  • Manta Technologies

Table of Contents

  • IBM i 7.3 Loses Standard Support On September 30, 2023
  • Software Supply Chain Attacks Are A Growing Threat
  • Guru: Aliases — Underused and Unappreciated
  • We Need Some Insight From You
  • IBM i PTF Guide, Volume 24, Number 40

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle