his it a good practice to declare variable outside a loop or repeat even if the use is only required in the loop.
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.
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.
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)
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)
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.
That's why I suggested to activate this through a command line parameter (or ini) ;-)
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.
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.)