LocalScope / EndLocalScope

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

LocalScope / EndLocalScope

Post 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...
I like logic, hence I dislike humans but love computers.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: LocalScope / EndLocalScope

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: LocalScope / EndLocalScope

Post 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..
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: LocalScope / EndLocalScope

Post 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".
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: LocalScope / EndLocalScope

Post 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.
I like logic, hence I dislike humans but love computers.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: LocalScope / EndLocalScope

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: LocalScope / EndLocalScope

Post 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;
}
I like logic, hence I dislike humans but love computers.
sancho2
User
User
Posts: 44
Joined: Wed Apr 15, 2015 5:14 am

Re: LocalScope / EndLocalScope

Post by sancho2 »

Joakim Christiansen wrote:It's not stupid, C++ has this
As does freebasic. I like the premise so +1.
Post Reply