× 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.



On Wed, Apr 1, 2009 at 3:54 AM, David FOXWELL <David.FOXWELL@xxxxxxxxx> wrote:
Hi people,

I'd like some comments concerning the use of constants. I've been asked to prepare a presentation to the team for a discussion on good coding practices, as the boss is getting tired of seeing this sort of thing :


http://systeminetwork.com/article/rpg-iv-style-21st-century

 * Global figurative constants
 *   Type relation = aunt
D gA          C            'A'

 myRelation = gA;


That's just plain silly. There's two reason to use named constants
instead of a hard coded literal or "magic number"
1) To give it a useful name that tells what the value means, ie. AUNT,
UNCLE, HOURS_PER_DAY
2) If the literal value ever needs to change there's only one place to
change it.

gA above doesn't meet objective #1.

Now, I'd have written :

D gAUNT       C            'A'

D gUNCLE      C            'U'

 myRelation = gAUNT;


Much better.

I tend to tie the constant value to the field:
d RELATION_AUNT
d c 'A'
d RELATION_UNCLE
d c 'U'

// Class Code 02: No Data
d SQL_STATE_NO_DATA...
d c '02000'
d SQL_STATE_NO_MORE_RS...
d c '02001'




Some questions :

I would normally not use a commentaire at the declaration of a constant. What do you think?

I'm sure there's exceptions to prove the rule, but by using an
appropriate name, you should never need a comment for a constant
declaration or a variable declaration. Added a comment for a group of
declarations is another matter, as in my example above.


We use a prefix g to indicate a globally defined constant, and w for a locally defined one. When would you locally define a constant in a sub procedure? I think it's nice to be able to see the definitions close to the calculation specs, but isn't a constant almost always global?


Unlike variables, there's no benefit to declaring a constant locally.
Most of my end up declared in a /COPY file with prototypes and
template declarations. I also have a STDCONST copy member that
contains constants that could be used in many places, HOURS_PER_DAY,
DAYS_PER_WEEK, ect. Any remaining get declared globally for the
module.

One could argue that a constant should never be local. As that would
allow you to have different definitions of it in different procedures.
Which defeats the purpose of a constant.

What is meant by figurative constant? I thought it was one with a fixed value like *BLANK.


You are correct, from the manual, "Figurative constants are implied
literals that allow specifications without referring to length:"

HTH,
Charles

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.