Page 1 of 1
Scope/EndScope keywords
Posted: Sun Aug 30, 2009 2:47 pm
by UserOfPure
I'd like to see Scope and EndScope keywords added -- should be easy! What they do is: the Scope one takes a single text parameter, and that text is (internally) added to the start of all variable names, procedure names, etc, from that point, until EndScope is reached.
What this means, is that you can copy and paste a snippet from these forums into your own app, wrap them with Scope/EndScope, and you are guaranteed that the variable names, procedure names, etc, from the snippet will NOT clash with your existing code at all (assuming you use a unique Scope name).
Posted: Sun Aug 30, 2009 3:44 pm
by akj
@UserOfPure:
How would global [and shared] variables be treated?
I doubt whether it would always be appropriate to prefix their names with the prefixed scope text.
Posted: Sun Aug 30, 2009 3:55 pm
by Kaeru Gaman
to avoid clash with own names, it's strongly recommended to use Protected inside of Procedures,
especially when you want to write portable includes.
additionally, just copying code from the forums into your own project without adjusting it is no deed of glory.
Posted: Sun Aug 30, 2009 9:39 pm
by UserOfPure
akj wrote:How would global [and shared] variables be treated?
In the same way. A global var named Joey in the snippet, with a scope parameter of Big, would just become a global var named BigJoey instead, meaning that it wouldn't clash with a main app global var named Joey. The snippet and main app would compile together without problem.
Posted: Mon Aug 31, 2009 10:09 am
by Blue
Kaeru Gaman wrote:[...]copying code from the forums into your own project without adjusting it is no deed of glory.
Couldn't have been said any better. And that's the crux of the matter.
For the life of me, i just can't think of a single situation when you'd want to do that.
If you want to test a snippet, you copy it into a new code window.
If you want to integrate the snippet into your code, you adjust and adapt it.
But to compile a snippet, as is, right into your existing code ? My good grief...
Why ?
Posted: Mon Aug 31, 2009 11:22 am
by UserOfPure
Kaeru Gaman wrote:it's strongly recommended to use Protected inside of Procedures
You can't use "Protected" with procedure names.
Blue wrote:to compile a snippet, as is, right into your existing code ? My good grief.
There are plenty of good standalone snippets in these forums by respected coders who write clean, efficient code for us to use. These such examples need no adaptions, and for you to say "good grief" about it means that you're basically trashing those good contributions by top coders here. I'm sure they appreciate being told that their efforts in the "Tricks N Tips" section are nothing more than crap.
I see no problem with allowing a drag-and-drop of such code into our apps. With Scope/EndScope, it just means we don't need to worry about adapting their code, or our code: just drop it in, and it's ready to run. No need to waste time picking the code apart and checking for clashing variable names and procedure names.
Posted: Mon Aug 31, 2009 12:22 pm
by Kaeru Gaman
UserOfPure wrote:You can't use "Protected" with procedure names.
There are plenty of good standalone snippets in these forums by respected coders who write clean, efficient code for us to use. These such examples need no adaptions, and for you to say "good grief" about it means that you're basically trashing those good contributions by top coders here. I'm sure they appreciate being told that their efforts in the "Tricks N Tips" section are nothing more than crap.
I see no problem with allowing a drag-and-drop of such code into our apps. With Scope/EndScope, it just means we don't need to worry about adapting their code, or our code: just drop it in, and it's ready to run. No need to waste time picking the code apart and checking for clashing variable names and procedure names.
/me shakes head
if you want to include codesnippets into your project, put it into an include and do a Find/Replace with
"Procedure " -> "Procedure Prefix_"
even with scope/endscope there is no guarantee that dragged-dropped code will work flawlessly.
you will have to check it over anyways.
if you want to code, put work into it.
if you don't want to put work into it, buy a click-together-kit and drop programming.
we did not say, you should not appreciate code from the forums, we just said
if you copy/paste it without investing the minimal work to put prefixes into it you do not appreciate it at all! 
Posted: Mon Aug 31, 2009 1:35 pm
by Trond
Search and replace is the digital manifestion of the devil.
Posted: Mon Aug 31, 2009 3:21 pm
by Kaeru Gaman
:roll: ei die katz die graue du ahnst es nicht...
Posted: Mon Aug 31, 2009 4:43 pm
by rsts
There is indeed much wonderful code in the forums.
But, as Mr Gaman mentioned, dropping it into your program and hoping it works as you expect it to, is potentially setting yourself up for problems down the road.
cheers
Posted: Mon Aug 31, 2009 7:23 pm
by blueznl
... which all has nothing to do with the request
+1
Although this sounds amazingly similar to the concept (and associated discussion) on modules and workspaces

Posted: Mon Aug 31, 2009 7:51 pm
by Demivec
Forgetting code posted in the forums for a moment, it would be useful to have a feature similar to this to make it easier to reuse my own code in other programs.
Prefixing every global variable and procedure and structure works but it has the failing that you can't improve a portion of code that is used in several different programs without the need to rename everything before it can be re-included in different codes. Choosing a great prefix from the start works well for code that is then included in different projects so that re-prefix everything is unnecessary, but it has the failing that it can make code much harder to read.
The reuse of code increases as the size of a project increases. If prefixing the names of variables, procedures, and structures is the common solution than why not turn all that repetitive action into a simple solution with a new keyword or two.
I am anxious for this to be implemented in some way, and Fred has said that it was something he would consider (look in the second link below from 2005). I am waiting for that item to advance on his ToDo list.
Note: this topic is simply a variation of namespaces and modules discussed in other threads and feature requests. Here are some links to those discussions.
http://www.purebasic.fr/english/viewtopic.php?t=36485
http://www.purebasic.fr/english/viewtopic.php?t=16224
http://www.purebasic.fr/english/viewtopic.php?t=32954
http://www.purebasic.fr/english/viewtopic.php?t=30163
http://www.purebasic.fr/english/viewtopic.php?t=25478
Posted: Tue Sep 01, 2009 11:23 pm
by Mistrel
This is all nice and everything but what point is there to scope/endscope without being able to pre-define a scope name? Like packages in Java and namespaces in c++?
Without being able to import a named scope this feature would very little use.