Scoped variables for readability and easier refactoring
Posted: Sun Oct 16, 2016 8:26 pm
I use EnableExplicit and Protected religiously to avoid bugs but scope has always bothered me.
Defining protected variables at the head of the function is all fine and good until you start having to list a lot of temporary stuff up there and other various trash variables. This makes it difficult to identify which variables of a procedure which are important and those which are not. This is taxing because I have to evaluate the code paths within a procedure to identify the lifetime of a variable rather than being able to simply examining its declared scope.
It is also especially frustrating when refactoring procedures and having protected declarations become orphaned. This requires manual consideration of each protected variable to verify whether it's still being used.
I would very much like to do this:
And especially this for loop variables:
The keyword doesn't have to be "Protected". It can be something like "Scoped" or "Local", etc. But I would very much like this functionality.
I understand that it's the compiler's discretion where to allocate memory and even if this were implemented, allocation of all variables in the procedure including trash variables might still occur in the same way. What I'm more concerned about is readability and ease of refactoring rather than enforcing memory allocation/deallocation within a scope.
Defining protected variables at the head of the function is all fine and good until you start having to list a lot of temporary stuff up there and other various trash variables. This makes it difficult to identify which variables of a procedure which are important and those which are not. This is taxing because I have to evaluate the code paths within a procedure to identify the lifetime of a variable rather than being able to simply examining its declared scope.
It is also especially frustrating when refactoring procedures and having protected declarations become orphaned. This requires manual consideration of each protected variable to verify whether it's still being used.
I would very much like to do this:
Code: Select all
Procedure This()
If Something
Protected a
;/ Blah Blah
EndIf
If SomethingElse
Protected a
;/ Blah Blah
EndIf
If SomethingElse
Protected some_trash1
Protected some_trash2
Protected some_trash3
;/ Blah Blah
EndIf
EndProcedure
Code: Select all
For Protected i to..
I understand that it's the compiler's discretion where to allocate memory and even if this were implemented, allocation of all variables in the procedure including trash variables might still occur in the same way. What I'm more concerned about is readability and ease of refactoring rather than enforcing memory allocation/deallocation within a scope.