Define var in loop or repeat

Posted by jason warren on 15-Dec-2016 15:07

his it a good practice to declare variable outside a loop or repeat even if the use is only required in the loop.

Posted by Matt Baker on 15-Dec-2016 15:38

This is a style preference.  The ABL compiler makes all variables scoped to the procedure/function/method in which they are defined.  So moving them to the top or leaving them defined inside of a block doesn't matter.

All Replies

Posted by Matt Baker on 15-Dec-2016 15:38

This is a style preference.  The ABL compiler makes all variables scoped to the procedure/function/method in which they are defined.  So moving them to the top or leaving them defined inside of a block doesn't matter.

Posted by Marco Mendoza on 16-Dec-2016 08:12

But, must be defined before is used.

PROCEDURE x:
display i.
def var i as int.
END PROCEDURE.

** Missing FOR, FIND or CREATE for a table with i in current block. (232) 
 First field name reference for a table scoped outside an internal         
 procedure or trigger must be qualified. (3510)                            
 **  Could not understand line 2. (196)

Posted by Jean-Christophe Cardot on 16-Dec-2016 08:17

Somebody knows if there is an enhancement request or if it has been permanently discarded to have the possibility to scope variables to a DO block? (may be activable through a parameter)

Posted by Patrick Tingen on 18-Dec-2016 14:18

I doubt if there is an enancement request for this, but even if there would be one, I doubt if it would get votes, since it is very likely to break old code with this, whereas the win from this would be one of style mostly.

Posted by Jean-Christophe Cardot on 19-Dec-2016 02:29

That's why I suggested to activate this through a command line parameter (or ini) ;-)

Posted by Thomas Mercer-Hursh on 19-Dec-2016 11:31

Yes, but if you want to accomplish this functionality, you can easily put the code in a procedure or method where the scoping is well understood.  There is little point to adding such functionality to a DO block since there is little scoped there, especially by default.

Posted by egarcia on 19-Dec-2016 12:46

This question reminded me of a similar scenario that happens in JavaScript.

In JavaScript, JSLint would return message "Move all 'var' declarations to the top of the function" or "Don't declare variables in a loop".

I personally would use this same approach for ABL (since it has a similar scope behavior).

Even though this would be a style preference, it is important that a program can clearly show the scope and what it is doing. (Using a command line parameter option to enable functionality would means that you would need to know the option for reading the code.)

For example, consider the following program.

How many times is the DISPLAY statement executed?

DEFINE VARIABLE i AS INTEGER NO-UNDO.

REPEAT i = 1 TO 2:

   DEFINE VARIABLE j AS INTEGER INITIAL 0 NO-UNDO.

   REPEAT WHILE j < 5:

       j = j + 1.

       DISPLAY i j.

   END.

END.

(Of course, there are different ways that the code could be written to make it clearer.)

This thread is closed