What is usually preferred in a sub-proc, INZ during variable declaration or using CLEAR?

170 Views Asked by At

Assuming both choices will yield same results, what choice do you usually make and why?

dcl-proc test_proc ;
   ...
   dcl-s count  int(5) inz ;
   ...
   // use count
end-proc ;

or

dcl-proc test_proc ;
   ...
   dcl-s count  int(5) ;
   ...
   clear count
   // use count
end-proc ;

Also, As the scope of variable count is local, do we even need to use either of them?

5

There are 5 best solutions below

1
Barbara Morris On BEST ANSWER

You don't need INZ to be able to use RESET.

But it's best to use RESET only when the RESET value would be different from the CLEAR value.

Using RESET requires the compiler to generate extra storage and extra instructions to save the initial value.

1
ojay On

Here are my views...

  • Pros of using INZ: can RESET it to 0 - as required going forward (i.e. better maintainability)
  • Cons of using CLEAR: one extra instruction
0
Charles On

RPG stand-a-lone variables are initialized to their defaults. For all numeric fields the default is 0.

Thus, for your local (and non-static) variable you don't need INZ nor use CLEAR or RESET prior to use.

RESET will always reset the variable to it's initialized value.
CLEAR will reset the variable to the type default value.

Examples:
dcl-s myNum int(5);

  • CLEAR myNum; --> myNum == 0
  • RESET myNum; --> myNum == 0

dcl-s myNum int(5) inz(5);

  • CLEAR myNum; --> myNum == 0
  • RESET myNum; --> myNum == 5

Addendum
The only thing I routinely use INZ on are data structures. Without INZ, the DS is initialized to blanks; regardless of the data types of the subfields. With INZ, each subfield is initialized to it's type default.

8
Victor Pomortseff On

In this particular case, I prefer the first option. Just out of habit, always initialize variables. If we are talking about global or static variables, then if necessary I use clear or reset. they can contain the value left from the last call in this activation group.

1
Buck Calabro On

I use INZ on every declaration.

Imagine adding a DS to a sub-proc that has no INZ keywords. I might not remember to INZ the DS, even though that's invariably necessary to get the 'natural' behaviour of the subfields.

I use CLEAR or RESET specifically to signal intent that something needing my focus is happening in the code - most often reusing a variable.