David,
 
I want to add another vote for Steve's answer: always use local variables
unless you have an obvious benefit from using a global variable for a
specific need.  The reason is human error; it's far too easy to think you
aren't using a certain global variable for anything at the moment, and so
you use it, only to find much later that under certain circumstances, you
have more than one sub trying to use the global variable in conflicting
ways.
 
There are computer languages (which I am all in favor of), which try to
prevent human error by making you do more design work, and more typing
initially; VBScript is absolutely not one of them, and it's easy to cause
accidental runtime errors using it; therefore, you have to do everything you
can to prevent accidental errors.
 
Chip
 


  _____  

From: Stephen Clower [mailto:[email protected]] 
Sent: Tuesday, June 26, 2012 7:15 AM
To: [email protected]
Subject: Re: yet another Object definition question


David,

There are two opinions regarding global variables. Some people use them all
the time while others do whatever they can to avoid them. In your example,
it would make more sense to keep track of a single FSO object rather than
recreating and destroying it, especially if it is going to be used in
multiple areas throughout your script. Generally, you should keep the scope
of your variables local to the functions where they are used, but if you
need to access a variable or object from many places, there's nothing wrong
with using a global variable here and there.

Regards,
Steve




  _____  

From: David <[email protected]>
Sent: Mon Jun 25 22:02:13 EDT 2012
To: [email protected]
Subject: yet another Object definition question


Throughout my app, I am going to use a given object several times, but in
different subs and functions, and with different properties and methods.
 
My techie question here is, what is the most adviceable way, or the official
practice here? Should I make a Global definition, and then do my local
variants thereof, like:
 
    Dim FS: Set FS = CreateObject( "WScript.FileSystemObject")
    Dim F: Set F = Nothing
sub ReadFile( Filename)
    Set F = FS.OpenTextFile( Filename, 1, False)
End Sub 'ReadFile.
Sub WriteFile( Filename)
    Set F = FS.OpenTextFile( Filename, 2, True)
End Sub 'WriteFile.
Sub EndApp()
    If Not F Is Nothing Then    Set F = Nothing
    Set FS = Nothing
End Sub 'EndAp.
 
Or, should I strictly go for local definitions, like:
    Dim FS: Set FS = CreateObject( "WScript.FileSystemObject")
Sub ReadFile( Filename)
    Dim F: Set F = FS.OpenTextFile( Filename, 1, False)
    Set  F = Nothing
End Sub 'ReadFile.
Sub WriteFile( Filename)
    Dim F: Set F = FS.OpenTextFile( Filename, 2, True)
    Set F = Nothing
End Sub 'WriteFile.
Sub EndApp()
    Set FS = Nothing
End Sub 'EndApp.
 
In my first sample, it does make the lesser amount of writing and number of
instructions. But is the second sample more "inline with the books"? Does
the one sample take less memory than the other? And, is the one more safe to
run - less vulnerable - than the other? I somehow have the feeling, that the
second sample - with all local definition of the F object, and nullifying it
for every time - is a bit more safe run. At the same time, I wonder if the
repeated definition of the object does take more time or memory - in any
way.
 
OK, I know time might not be the biggest issue, since we are talking only
fractions of a second anyway. But what if the above was holding objects that
are called hundreds of times, like in a speech handler of some kind? The,
even fractions of seconds that you save every time you call an object could
maybe have an effect? And, what is the encouraged practice in a matter like
this. I want to have the app running as safe and fast as possible, smile.
 
Thanks for any feedback here.
 

--
Stephen Clower
App Development & Product Support
Gw Micro, Inc.
Sent from my phone.


Reply via email to