Page 1 of 1
LocalScope / EndLocalScope
Posted: Thu Mar 03, 2011 6:36 pm
by Joakim Christiansen
I often find myself putting code into procedures to keep its variables within its own scope, but I tend to do this even for code that doesn't need to be inside a procedure just because of this!
So why not something as simple and nice as this:
Code: Select all
Define x,y,someValue=10
LocalScope
Define x,y ;should use local x,y instead of those defined outside
For x=0 To 320
For y=0 To 240
;whatever
Next
Next
Debug someValue ;should debug "10"
EndLocalScope
It would make my code flow more readable because I wouldn't have to relocate code as much...
Re: LocalScope / EndLocalScope
Posted: Thu Mar 03, 2011 9:13 pm
by skywalk
Hi,
Not sure if I agree with you here.
Better that your main code only call Procedure()'s.
And they do the work of "LocalScope" for you without new keywords.
Code: Select all
Init()
Main()
End
;DataSections...
I do use mainline code for debugging individual routines.
Re: LocalScope / EndLocalScope
Posted: Fri Mar 04, 2011 11:09 pm
by Zach
Joakim Christiansen wrote:I often find myself putting code into procedures to keep its variables within its own scope, but I tend to do this even for code that doesn't need to be inside a procedure just because of this!
So why not something as simple and nice as this:
Code: Select all
Define x,y,someValue=10
LocalScope
Define x,y ;should use local x,y instead of those defined outside
For x=0 To 320
For y=0 To 240
;whatever
Next
Next
Debug someValue ;should debug "10"
EndLocalScope
It would make my code flow more readable because I wouldn't have to relocate code as much...
I'm not sure what you are trying to illustrate?
If you don't want to overwrite x, y - good practice says use different names?
Wouldn't it be more efficient to restrict yourself to only using certain variable names as Temp/Garbage/throw-away variables? Like in your example when you need to loop something?
That's what I try to do I guess.. I also try to give my variables meaningful, but terse, names. It saves the simple a...z singletons for temp/trash type calculations, while keeping actual data in more clearly stored destinations..
Re: LocalScope / EndLocalScope
Posted: Sat Mar 05, 2011 1:35 am
by MachineCode
Zach wrote:If you don't want to overwrite x, y - good practice says use different names?
That's what I was thinking! But even using procedures does exactly what he wants, so I don't get the request. Procedure/EndProcedure = LocalScope/EndLocalScope when it comes to preventing variable clashes. But just using proper variable namings would solve the problem instantly. Anything global or important should be named that way, like "glob_X" instead of just "x".
Re: LocalScope / EndLocalScope
Posted: Sat Mar 05, 2011 2:04 am
by Joakim Christiansen
To explain why it would be nice I guess I need to show some better examples... But for now I am waiting to see if there is actually someone in here who agrees with me.
good practice says use different names?
Actually I want to avoid having to be super creative just to avoid accidental overwriting of variables that shouldn't be touched by some code.
Re: LocalScope / EndLocalScope
Posted: Sat Mar 05, 2011 2:32 am
by skywalk
Joakim Christiansen wrote:Actually I want to avoid having to be super creative just to avoid accidental overwriting of variables that shouldn't be touched by some code.
EnableExplicit
and
[Find in files...]
That will list every possible entry you have for the variable.
Waiting for the tool automation in 4.6++, then you could even filter the search results to only "define" or "protected" or "dim" "myvariable", etc.
Re: LocalScope / EndLocalScope
Posted: Tue May 19, 2015 12:30 am
by Joakim Christiansen
It's not stupid, C++ has this, check out the third code example on this page:
http://www.cplusplus.com/doc/tutorial/namespaces/
Code: Select all
// inner block scopes
#include <iostream>
using namespace std;
int main () {
int x = 10;
int y = 20;
{
int x; // ok, inner scope.
x = 50; // sets value to inner x
y = 50; // sets value to (outer) y
cout << "inner block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
}
cout << "outer block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
return 0;
}
Re: LocalScope / EndLocalScope
Posted: Tue May 19, 2015 2:01 am
by sancho2
Joakim Christiansen wrote:It's not stupid, C++ has this
As does freebasic. I like the premise so +1.