Board index » Visual Studio » Global Variables - A problem?
|
KidGlove
|
|
KidGlove
|
Global Variables - A problem?
Visual Studio118
Some time ago a poster "ranted" about not using public/global variables. Ocassionaly I use them - but it's clear they are global (like blnPublicStatusFlag) What are the pros and cons? (If pro is the opposite of con, is progress the opposit of congress? :-) Thanks Mark - |
| Jane
Registered User |
Wed Mar 03 09:18:54 CST 2004
Re:Global Variables - A problem?
In article <#BMY6hSAEHA.2448@TK2MSFTNGP12.phx.gbl>, Mark Durrenberger
<durrenm@yahoo.com>writes QuoteSome time ago a poster "ranted" about not using public/global variables. basic, static type information as parameters doesn't make sense. Read the stuff from the database once and store in well commented global variables in the 'initialise' routine. In 'worker' functions/routines, I always pass the 'input' in as parameters and use local variables for stuff that will not be 'used' outside of the routine. Even the all singing all dancing OO technology that rants against global variables has them . . . if class variables and instance variables are not a 'global', then pigs can fly. -- Jane Ransom in Lancaster. If you need to email me for any other reason, put ransoms at jandg dot demon dot co dot uk where you see ransom@deadspam.com - |
| Jan
Registered User |
Wed Mar 03 09:38:36 CST 2004
Re:Global Variables - A problem?
"Mark Durrenberger" <durrenm@yahoo.com>'s wild thoughts were
released on Wed, 3 Mar 2004 09:04:00 -0500 bearing the following fruit: QuoteSome time ago a poster "ranted" about not using public/global variables. decision to split it up. This is *far* more difficult if your app contains a lot of public variables. Your code is not sharable, often you find you could use a routine, form, module etc in another project. If everything is written like a black box (everything gets passed in) then you can share a component with any other project slashing development time. If you rely on global variables you would then not only have to add all these into the new project, but set them too. When you write a routing where everything it needs is passed in it's far easier to debug. If it relies on globals it can be very hard to find out where that global ended up with a suprising value. Read up on object orientated programming (OOP) for more information. -- Jan Hyde (MVP - Visual Basic) A chorus girl gets her education by stages, a college girl by degrees. (The International Save the Pun Foundation) [Abolish the TV Licence - www.tvlicensing.biz/]">www.tvlicensing.biz/] - |
| Jim
Registered User |
Wed Mar 03 09:40:10 CST 2004
Re:Global Variables - A problem?
I don't see how anyone can rant about them or advise against
them. After all, App is a global variable and there are many other global types. All the type declarations, Long, Integer, Double, Single, are all global definitions. I tend to mark my own global variables with a g- prefix. Form wide variables are marked with an m- prefix. And all localized variables are marked with str-, lng-, i-, etc Hungarian prefixes (almost all). This tends to make the code very easy to read at any date in the future and helps when diagnosing problems. Now I've run into a few problems with one thing in particular, and I think alot of folks run into this same problem. Anywhere that an error can be trapped, I tend to use: Private Sub subDoTheStuff() On Error Goto LocalErr '...code here to do what's needed. '...code '...code Exit Sub 'or Exit Function LocalErr: gStrErr = ERRMSG & CStr(Err.Number) & vbCrLf & Err.Description gSubError gStrErr, "modName/subDoTheStuff" End Sub The problems I've run into with the LocalErr thing above, include the possibility that an error can occur in a routine that has no trapping set up. It's rare, but it can happen and it might make diagnosing and troubleshooting things a little tricky. So my thoughts on this subject are to always set up an error trap if there's any possibility at all of some error that might occur. Hope that helps. -- Jim Carlock www.microcosmotalk.com/">www.microcosmotalk.com/ Post replies to the newsgroup. "Mark Durrenberger" <durrenm@yahoo.com>wrote in message Some time ago a poster "ranted" about not using public/global variables. Ocassionaly I use them - but it's clear they are global (like blnPublicStatusFlag) What are the pros and cons? (If pro is the opposite of con, is progress the opposit of congress? :-) Thanks Mark - |
| Jim
Registered User |
Wed Mar 03 09:54:46 CST 2004
Re:Global Variables - A problem?
Or add a database to the project.
E.g. Microsoft Registry. Almost everything in the registry is a global variable. <g> -- Jim Carlock www.microcosmotalk.com/">www.microcosmotalk.com/ Post replies to the newsgroup. "Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com>wrote in message "Mark Durrenberger" <durrenm@yahoo.com>'s wild thoughts were released on Wed, 3 Mar 2004 09:04:00 -0500 bearing the following fruit: QuoteSome time ago a poster "ranted" about not using public/global variables. decision to split it up. This is *far* more difficult if your app contains a lot of public variables. Your code is not sharable, often you find you could use a routine, form, module etc in another project. If everything is written like a black box (everything gets passed in) then you can share a component with any other project slashing development time. If you rely on global variables you would then not only have to add all these into the new project, but set them too. When you write a routing where everything it needs is passed in it's far easier to debug. If it relies on globals it can be very hard to find out where that global ended up with a suprising value. Read up on object orientated programming (OOP) for more information. -- Jan Hyde (MVP - Visual Basic) A chorus girl gets her education by stages, a college girl by degrees. (The International Save the Pun Foundation) [Abolish the TV Licence - www.tvlicensing.biz/]">www.tvlicensing.biz/] - |
| Bob
Registered User |
Wed Mar 03 10:04:57 CST 2004
Re:Global Variables - A problem?
"Jane Ransom" <ransoms@deadspam.com>wrote in message
<cut> QuoteEven the all singing all dancing OO technology that rants against particular procedure and there will be nothing 'global' about it. -- Reply to the group so all can participate VB.Net... just say "No" - |
| Bob
Registered User |
Wed Mar 03 10:11:01 CST 2004
Re:Global Variables - A problem?
"Jim Carlock" <anonymous@127.0.0.1>wrote in message
QuoteI don't see how anyone can rant about them or advise against something in your own app as global but I've learned the hard way that while it can seem like an easier approach in the initial coding it can make maintenance more difficult if global variables are overused. They prevent effective encapsulation of functionality making it harder to create reusable objects and code. Like most things they are not inherently 'evil' (only END is evil) and there are cases where they may be appropriate but any time you are making something global you should probably consider other approaches that don't tie your components to external data. Quoteand there are many other you create a class or a form or a public procedure in a BAS module you are creating something that is available globally just like the data types you mention are available globally. -- Reply to the group so all can participate VB.Net... just say "No" - |
| Don
Registered User |
Wed Mar 03 10:17:32 CST 2004
Re:Global Variables - A problem?
I like flying pigs... ;)
How ever, I never use Global Varibles... I only use Public ones.... <BeG> I'm not an advocate of using many many Public varibles even though I do use them a lot. It was how I learned to program (non-winders os) and it has just migrated along with me. There are a few reasons, valid ones, not to use them if you can do it else wise using good programming techniques and I'm sure others will mention most of them so I'll not venture into them here... On Wed, 3 Mar 2004 15:18:54 +0000, Jane Ransom <ransoms@deadspam.com>wrote: QuoteIn article <#BMY6hSAEHA.2448@TK2MSFTNGP12.phx.gbl>, Mark Durrenberger Don - |
| Ralph
Registered User |
Wed Mar 03 10:19:35 CST 2004
Re:Global Variables - A problem?"Mark Durrenberger" <durrenm@yahoo.com>wrote in message QuoteSome time ago a poster "ranted" about not using public/global variables. Jane pointed out the major problem with globals, when she noted that when used for 'universal' reads (eg, the App object) globals are generally benign. However, they become problematic when 'reads' and 'writes' are involved, for then you enter into the realm of concurrency and validity. It is not wise to have far-flung routines chewing on the same animal. Even when you feel you have good control over who or what is writing and who or what is reading - even simple changes to your code can result in an application-wide hunt and repair. Also 'globals' by their very nature imply external data and routines to manage them, leading one to procedural or data-centric solutions. Again benign when used for 'reads', a misery waiting-to-happen if used for volatile storage. An object that is dependent on external data - is a poor object indeed. HTH -ralph - |
| Rick
Registered User |
Wed Mar 03 10:20:43 CST 2004
Re:Global Variables - A problem?QuoteLike most things they are not inherently 'evil' (only END is evil) End If, End While, End Select, End Type <g>. Rick - |
| Jim
Registered User |
Wed Mar 03 10:28:03 CST 2004
Re:Global Variables - A problem?
"Rick Rothstein" wrote:
Quote>Like most things they are not inherently 'evil' (only END is evil) End Enum too! -- Jim Carlock www.microcosmotalk.com/">www.microcosmotalk.com/ Post replies to the newsgroup. - |
| Rick
Registered User |
Wed Mar 03 10:44:59 CST 2004
Re:Global Variables - A problem?Quote>>Like most things they are not inherently 'evil' (only END is evil) also. Rick - |
| Bob
Registered User |
Wed Mar 03 10:56:50 CST 2004
Re:Global Variables - A problem?
"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net>wrote in message
Quote>Like most things they are not inherently 'evil' (only END is evil) -- Reply to the group so all can participate VB.Net... just say "No" - |
| Larry
Registered User |
Wed Mar 03 12:53:13 CST 2004
Re:Global Variables - A problem?
"Bob Butler" <tiredofit@nospam.com>wrote
Quote"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net>wrote Touché <g> LFS - |
| Jane
Registered User |
Wed Mar 03 11:24:01 CST 2004
Re:Global Variables - A problem?
In article <exAsIlTAEHA.712@tk2msftngp13.phx.gbl>, Bob Butler
<tiredofit@nospam.com>writes Quote"Jane Ransom" <ransoms@deadspam.com>wrote in message I call that pretty global in nature!!!! -- Jane Ransom in Lancaster. If you need to email me for any other reason, put ransoms at jandg dot demon dot co dot uk where you see ransom@deadspam.com - |
| Jane
Registered User |
Wed Mar 03 11:24:55 CST 2004
Re:Global Variables - A problem?
In article <nf0c409nvu70pd1q1ubneoen8rdvtcctq3@4ax.com>, Don@home.com
writes QuoteI like flying pigs... ;) Jane Ransom in Lancaster. If you need to email me for any other reason, put ransoms at jandg dot demon dot co dot uk where you see ransom@deadspam.com - |
| Mark
Registered User |
Wed Mar 03 13:29:15 CST 2004
Re:Global Variables - A problem?
Wow go away from your desk for a few hours and look what happens...
Great thread thanks for all the comments Quote
though I'd have not mixed my metaphors to say it. Can someone tell me how to write a routine that 'chews' ? ;-) Thanks, Mark - |
| Bob
Registered User |
Wed Mar 03 13:35:53 CST 2004
Re:Global Variables - A problem?
"Jane Ransom" <ransoms@deadspam.com>wrote in message
<cut> QuoteAn instance variable is available to all methods in an instance of a available to anything in the class but not outside the class unless it is public. The class itself still encapsulates that data and there should only be module-level variables for items that are needed in multiple procedures inside the class. It's a big step down from application global. QuoteA class variable is available to all instances of a class. -- Reply to the group so all can participate VB.Net... just say "No" - |
| Jim
Registered User |
Wed Mar 03 13:50:26 CST 2004
Re:Global Variables - A problem?
Public variables are like public restrooms.
Wipe the seat before you sit on them! -- Jim Carlock www.microcosmotalk.com/">www.microcosmotalk.com/ Post replies to the newsgroup. "Bob Butler" <tiredofit@nospam.com>wrote in message "Jane Ransom" <ransoms@deadspam.com>wrote in message <cut> QuoteAn instance variable is available to all methods in an instance of a available to anything in the class but not outside the class unless it is public. The class itself still encapsulates that data and there should only be module-level variables for items that are needed in multiple procedures inside the class. It's a big step down from application global. QuoteA class variable is available to all instances of a class. -- Reply to the group so all can participate VB.Net... just say "No" - |
| Larry
Registered User |
Wed Mar 03 13:57:31 CST 2004
Re:Global Variables - A problem?
"Jane Ransom" <ransoms@deadspam.com>wrote
Quote>How are they global? a class instance variable can be defined local to a I think you are are confused, either of what to call the different variables, or of their actual operation. Taken one at a time: QuoteAn instance variable is available to all methods in an instance of a Set cla = New Class1 'cla' is the instance variable, and it will not be visible to the methods in that class. QuoteA class variable is available to all instances of a class. Properties, and Methods (Subs and Functions) What is a class variable? LFS - |
| Larry
Registered User |
Wed Mar 03 13:59:14 CST 2004
Re:Global Variables - A problem?"Mark Durrenberger" <durrenm@yahoo.com>wrote QuoteGreat thread thanks for all the comments Take a routine that massages data, and add a little bite to it! <g> LFS - |
| Bob
Registered User |
Wed Mar 03 13:58:43 CST 2004
Re:Global Variables - A problem?
Bob Butler wrote:
QuoteLike most things they are not inherently 'evil' (only END Type Coercion is evil. Option Base is evil; unspecified lower array bounds are at least naughty. String concatenation using the addition operator is evil. Flag-bit gathering using the addition operator is evil. (and the list goes on...) Bob -- looking for work again <obob.com/bob/resume/>">obob.com/bob/resume/> - |
| Mark
Registered User |
Wed Mar 03 14:18:31 CST 2004
Re:Global Variables - A problem?Quote
massage code? <grin> - |
| Jane
Registered User |
Wed Mar 03 15:13:48 CST 2004
Re:Global Variables - A problem?
In article <e45wAbVAEHA.3004@TK2MSFTNGP10.phx.gbl>, Bob Butler
<tiredofit@nospam.com>writes Quote"Jane Ransom" <ransoms@deadspam.com>wrote in message If you look back at my original post, I am talking pure OO as in Smalltalk and Java. Are we allowed to post Java code in this group? -- Jane Ransom in Lancaster. If you need to email me for any other reason, put ransoms at jandg dot demon dot co dot uk where you see ransom@deadspam.com - |
| Jane
Registered User |
Wed Mar 03 15:14:33 CST 2004
Re:Global Variables - A problem?
In article <eFplXkVAEHA.3308@TK2MSFTNGP10.phx.gbl>, Larry Serflaten
<serflaten@usinternet.com>writes Quote
I'm sorry . . . I'll go back in to my shell and belt up !!! -- Jane Ransom in Lancaster. If you need to email me for any other reason, put ransoms at jandg dot demon dot co dot uk where you see ransom@deadspam.com - |
| Bob
Registered User |
Wed Mar 03 15:14:34 CST 2004
Re:Global Variables - A problem?
"Bob O`Bob" <filterbob@yahoogroups.com>wrote in message
QuoteBob Butler wrote: <g> -- Reply to the group so all can participate VB.Net... just say "No" - |
| Bob
Registered User |
Wed Mar 03 15:15:12 CST 2004
Re:Global Variables - A problem?
"Mark Durrenberger" <durrenm@yahoo.com>wrote in message
Quote>Take a routine that massages data, and add a little bite to it! -- Reply to the group so all can participate VB.Net... just say "No" - |
| Bob
Registered User |
Wed Mar 03 15:20:22 CST 2004
Re:Global Variables - A problem?
"Jane Ransom" <ransoms@deadspam.com>wrote in message
QuoteIn article <e45wAbVAEHA.3004@TK2MSFTNGP10.phx.gbl>, Bob Butler OO is fine by me but if you mentioned that I missed it. As for having knickers in a twist I'm afraid that's not very likely as I don't even own any knickers! <g> -- Reply to the group so all can participate VB.Net... just say "No" - |
| Mark
Registered User |
Wed Mar 03 15:22:04 CST 2004
Re:Global Variables - A problem?
Careful, I'm a big fan...
QuoteNo, just the most *incredibly*, mind-numbingly boring code you ever saw. - |
| Bob
Registered User |
Wed Mar 03 15:29:19 CST 2004
Re:Global Variables - A problem?
Bob Butler wrote:
Quote>Type Coercion is evil. It certainly is evil when _Microsoft_ actively promotes code samples which do those nasty things, causing many neophytes to accept and learn those "worst practices" as if they were best. Bob -- looking for work again <obob.com/bob/resume/>">obob.com/bob/resume/> - |
| Bob
Registered User |
Wed Mar 03 18:28:27 CST 2004
Re:Global Variables - A problem?
"Bob O`Bob" <filterbob@yahoogroups.com>wrote in message
QuoteBob Butler wrote: was (past tense since it's getting harder and harder to find now and even bad examples were better than none!) -- Reply to the group so all can participate VB.Net... just say "No" - |
| Karl
Registered User |
Wed Mar 03 18:51:16 CST 2004
Re:Global Variables - A problem?
Bob Butler <tiredofit@nospam.com>wrote:
Quote"Bob O`Bob" <filterbob@yahoogroups.com>wrote in message -- [Microsoft Basic: 1976-2001, RIP] - |
| Bob
Registered User |
Wed Mar 03 20:40:16 CST 2004
Re:Global Variables - A problem?
"Karl E. Peterson" <karl@mvps.org>wrote in message
QuoteBob Butler <tiredofit@nospam.com>wrote: hidden better than that. -- Reply to the group so all can participate VB.Net... just say "No" - |
| Joe
Registered User |
Wed Mar 03 23:47:32 CST 2004
Re:Global Variables - A problem?
"Bob Butler" <tiredofit@nospam.com>wrote in message <news:#gBzKIZAEHA.4080@TK2MSFTNGP09.phx.gbl>...
Quote"Karl E. Peterson" <karl@mvps.org>wrote in message but the bad coding practices rampant in their code samples still have tactical value in leading VB Classic programmers astray and encouraging the notion that good code can be written only in B#. -- Joe Foster <mailto:jlfoster%40znet.com>Sign the Check! <www.xenu.net/>">www.xenu.net/> WARNING: I cannot be held responsible for the above They're coming to because my cats have apparently learned to type. take me away, ha ha! - |
| Ralph
Registered User |
Wed Mar 03 23:54:54 CST 2004
Re:Global Variables - A problem?"Mark Durrenberger" <durrenm@yahoo.com>wrote in message QuoteWow go away from your desk for a few hours and look what happens... My hoosier (the land of mixed-metaphors) roots are often exposed. I like the words massage and mangle as well. Too many years teaching OO to newbies I guess. I mean how many times can you say 'mutable' before people's eyes start to glaze? -ralph - |
| Ralph
Registered User |
Thu Mar 04 00:53:04 CST 2004
Re:Global Variables - A problem?"Jane Ransom" <ransoms@deadspam.com>wrote in message QuoteIn article <exAsIlTAEHA.712@tk2msftngp13.phx.gbl>, Bob Butler then I guess you could call them global. Just as you can say an automatic variable is 'global' to the procedure in which it is defined. However, I don't believe that is what the original inquirer had in mind. The implementation of a instance varible mimics in many ways the definition of a static variable within a procedure (in fact, in some languages they are defined as "static") only with wider scope. However they may be implemented - references and access to them is very clearly defined and limited, thus managed as a collaboration between objects and ownership. More importantly - the same warning is in effect for their use - Instances variables are relatively benign when used for 'reads' but carry subtle opportunities for disaster when used for reads and writes. -ralph - |
| Bob
Registered User |
Thu Mar 04 08:25:21 CST 2004
Re:Global Variables - A problem?
"Ralph" <nt_consulting32@hotmail.com>wrote in message
<cut> QuoteToo many years teaching OO to newbies I guess. I mean how many times -- Reply to the group so all can participate VB.Net... just say "No" - |
| Stefan
Registered User |
Thu Mar 04 12:13:05 CST 2004
Re:Global Variables - A problem?
On Wed, 3 Mar 2004 16:28:27 -0800, "Bob Butler"
<tiredofit@nospam.com>wrote: in <Oa4Fg#XAEHA.712@tk2msftngp13.phx.gbl> Quote"Bob O`Bob" <filterbob@yahoogroups.com>wrote in message for a call to PSS... - |
