Tips and Techniques: Declaring Work Fields

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

One of the problems I see over and over again is that programmers declare work fields immediately following a data structure, program described or externally described. The code example shown below is very common, and it often causes several hours--and sometimes days--of confusion and debugging.

One of the most common mistakes is an omission when declaring work fields outside of data structures. Often, a key identifier is overlooked and, because it is not intuitive, a programmer may not notice it.

So what's wrong with this code? Technically speaking, nothing. The data structures CUSTOMER and ITEMS are valid, as is the COUNT field. Or is it?


.....DName+++++++++++EUDS.......Length+TDc.Functions++++++
D Customer E DS ExtName(CUSTMAST)
D Count 10I 0
D Items E DS ExtName(ItemMast) Occurs(50)

In the code in the above example, there is a data structure named CUSTOMER that is externally described. All subfields in the CUSTOMER data structure come from the field descriptions of the CUSTMAST file. Next, the COUNT field is declared. In this case, it is declared with the intent of it being a work field. However, as it is coded, it is actually declared as an additional subfield for the CUSTOMER data structure.

What is missing from the original Definition specification is the inclusion of the Definition Type in the COUNT definition. (Don't confuse Definition Type with Data Type. They are two different components.) Without this Definition Type, COUNT is considered just another subfield of the CUSTOMER data structure and is not an independent or standalone field.

The COUNT field's correct definition is as follows:

.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++
     D count         S               10I 0

Note the "S" in position 22. This indicates that the field is a standalone field and is not part of a data structure.

The original statements, rewritten correctly, are as follows:


.....DName+++++++++++EUDS.......Length+TDc.Functions+++++++++
D Customer E DS ExtName(CUSTMAST)
D count S 10I 0
D Items E DS ExtName(ItemMast) Occurs(50)

Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: