Define vs Protected in procedures

Just starting out? Need help? Post your questions and find answers here.
mikejs
Enthusiast
Enthusiast
Posts: 175
Joined: Thu Oct 21, 2010 9:46 pm

Define vs Protected in procedures

Post by mikejs »

Quick question - I've got into the habit of using Define for local variables at the start of a procedure, but I notice from the help and other code examples that people often use Protected instead. So, where I have:

Code: Select all

Procedure Something(param.l)
Define somevar.l
; do stuff...
EndProcedure
Others might have:

Code: Select all

Procedure Something(param.l)
Protected somevar.l
; do stuff...
EndProcedure
Is Protected a better approach to use, and if so why? Also, would there by any issues in doing a search and replace for these on an existing program?

(The help mentions that Protected avoids problems where a global variable matches a local one - in my own code I always use a different naming convention for globals, so this doesn't arise.)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Define vs Protected in procedures

Post by ts-soft »

Define makes no sense in a procedure. You should only use Protected, Static, Shared, Dim, NewList ...
but not: Global, Define in a procedure. You can do this, but is not a good style.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
mikejs
Enthusiast
Enthusiast
Posts: 175
Joined: Thu Oct 21, 2010 9:46 pm

Re: Define vs Protected in procedures

Post by mikejs »

Ok, thanks for that, it's rather the answer I was expecting.

Would you expect any issues with just doing a search and replace on my existing programs?

Also - no globals in procedures? I currently tend to have an InitGlobals() that gets called early on and does all the Global stuff I need doing. Is this also bad style?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Define vs Protected in procedures

Post by luis »

Probably it's better to avoid the use of define inside a proc, for clarity.

With 'protected', you are making clear to the reader a certain var is local to the proc no matter what, even if you declare the var when a global with the same name is already declared.

In fact, if you try to use 'define' to do the same you get an error (something about same var already declared with a different scope).

Excluding this case, 'define' and 'protected' seems to act the same way inside a proc, the code generated is the same for what I saw.
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Define vs Protected in procedures

Post by luis »

mikejs wrote: Would you expect any issues with just doing a search and replace on my existing programs?
Depend on how you used define, it can be used to define defaults type for example, so you can't substitute that blindly, or you can have used it outside a proc, again you can't substitute that blindly. I would never do a blind search/replace.

mikejs wrote: Also - no globals in procedures? I currently tend to have an InitGlobals() that gets called early on and does all the Global stuff I need doing. Is this also bad style?
It's generally considered "bad" to have globals scattered everywhere in the program, for obvious reasons, if you put them in a single place easy to find and maintain I don't see a problem.
"Have you tried turning it off and on again ?"
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Define vs Protected in procedures

Post by ts-soft »

mikejs wrote:Also - no globals in procedures? I currently tend to have an InitGlobals() that gets called early on and does all the Global stuff I need doing. Is this also bad style?
If you define all globals in one procedure, it is not a bad style.
All define of globals should in one place, "near the top" of source, for clarity.
"near the top" = After or before "IncludeFiles" for Scooping!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Define vs Protected in procedures

Post by eesau »

ts-soft wrote:
mikejs wrote:Also - no globals in procedures? I currently tend to have an InitGlobals() that gets called early on and does all the Global stuff I need doing. Is this also bad style?
If you define all globals in one procedure, it is not a bad style.
All define of globals should in one place, "near the top" of source, for clarity.
"near the top" = After or before "IncludeFiles" for Scooping!
It's also a very good idea to put all globals inside a globally declared structure, that way you'll get some additional benefits from the autocomplete feature.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Define vs Protected in procedures

Post by Trond »

eesau wrote:
ts-soft wrote:
mikejs wrote:Also - no globals in procedures? I currently tend to have an InitGlobals() that gets called early on and does all the Global stuff I need doing. Is this also bad style?
If you define all globals in one procedure, it is not a bad style.
All define of globals should in one place, "near the top" of source, for clarity.
"near the top" = After or before "IncludeFiles" for Scooping!
It's also a very good idea to put all globals inside a globally declared structure, that way you'll get some additional benefits from the autocomplete feature.
And also your program will be easier to extend. Assume your program handles documents. If you use global variables for the document information, you'll have a hard time changing the program to allow multiple documents at a time (like in a tabbed interface). If you have globals in a structured variable, it will be easier.
User avatar
skywalk
Addict
Addict
Posts: 4260
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Define vs Protected in procedures

Post by skywalk »

I noticed a while back that Structures defined in Procedures are actually global.
Why is that?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Define vs Protected in procedures

Post by ts-soft »

I have never seen a local structure :wink:
Structure, Prototype, Constant are allways Global and don't put it in a procedure, is a bad style.

This are no variables!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4260
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Define vs Protected in procedures

Post by skywalk »

ts-soft wrote:I have never seen a local structure :wink:
Structure, Prototype, Constant are allways Global and don't put it in a procedure, is a bad style.

This are no variables!
I agree, so shouldn't it be flagged as a Syntax Error by the compiler?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Define vs Protected in procedures

Post by ts-soft »

skywalk wrote:I agree, so shouldn't it be flagged as a Syntax Error by the compiler?
bad style <> Syntax Error.
Give the user the freedom to decide for himself
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4260
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Define vs Protected in procedures

Post by skywalk »

Fair enough.
I'll put a request for the Manual pages to state "Structures defined in Procedures are always Global".
It seems weird to allow this and I don't see the point?

Code: Select all

Procedure x()
  Structure sY
    i.i
  EndStructure
EndProcedure

Procedure Y()
  Structure sY   ;<---[COMPILER] Line 8: 'Structure', 'Interface' or 'Prototype' already declared: sY.
    i.i
  EndStructure
EndProcedure

Debug Y()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Define vs Protected in procedures

Post by eesau »

skywalk wrote:Fair enough.
I'll put a request for the Manual pages to state "Structures defined in Procedures are always Global".
It seems weird to allow this and I don't see the point?
PureBasic structures aren't global, they're scopeless (the same is with constants). Scoped structures might make sense but I think there are more useful things to implement to the language (like modules/namespaces).
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Define vs Protected in procedures

Post by Foz »

Ok, on a very similar theme then:

if we have the Global keyword for global variables, and the Protected keyword for local variables, why should we use Define at all?

Or to put it another way, what is the difference between Define and Global?
Post Reply