× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Constants are always better than hard-coding the value, so that's a great job in
and of itself.
Using %ELEM() in your code is more clear, only because cCMLimit is an ambiguous
name, as re fields with special symbols in them, such as CM# and IN#.
Certainly %elem(CM#) is clear that you're using the number of declared elements
for the array CM#, but what does that array contain? 

Another approach (not a better one, but one of equal honor) is to use %ELEM()
everywhere instead of a const. For example:

D cCMLimit        C                   Const(20)
D CM#             S              7  0 Dim(cCMLimit)
D IN#             S              7  0 Dim(%elem(cm#))
D AMT             S              7  2 Dim(%elem(cm#))
Or...
D CM#             S              7  0 Dim(20)
D IN#             S              7  0 Dim(%elem(cm#))
D AMT             S              7  2 Dim(%elem(cm#))

Then in your code also use %elem(cm#). 
The benefit of this method, is that it directly says:
"Arrays IN# and AMT have the same number of elements as CM# and this was done on
purpose and is a requirement." 
Using the constant says that the element count is the same, but it doesn't
directly indicate that the arrays are associated with one another.
Just my 2 cents.


-Bob Cozzi
www.iSeriesTV.com
Ask your Manager to watch iSeriesTV.com


-----Original Message-----
From: rpg400-l-bounces+cozzi=rpgiv.com@xxxxxxxxxxxx
[mailto:rpg400-l-bounces+cozzi=rpgiv.com@xxxxxxxxxxxx] On Behalf Of Jeff Crosby
Sent: Thursday, August 17, 2006 9:48 AM
To: 'RPG programming on the AS400 / iSeries'
Subject: %Elem(Array) vs. cCMLimit

This isn't the most important question of our times, but I just wondered
what everyone else thought.

If I need to define more than one array, each array with the same number of
elements, I typically do this:

D cCMLimit        C                   20

D CM#             S              7  0 Dim(cCMLimit)
D IN#             S              7  0 Dim(cCMLimit)
D AMT             S              7  2 Dim(cCMLimit)

Then, later in my code, I always use the constant I created, like this:

For X = 1 To cCMLimit;
  <do something>
Endfor;

Or this:

If X <= cCMLimit;
  <do something>
Else;
  <do something else>
Endif;

It suddenly occurred to me that even though I used a constant to define the
size of the array, in the code I should still use %Elem like this:

For X = 1 To %Elem(CM#);
  <do something>
Endfor;

Because this makes it immediately apparent to any other programmer (or me 6
months down the road) that I'm traversing the entire array, whereas using a
constant doesn't imply that.

What do you think?

Enquiring minds want to know . . .


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.