#150491 - 2005-10-24 09:55 PM
Question on Best Practices
|
rwright74
Lurker
Registered: 2005-04-10
Posts: 3
|
Can someone explain why according to "Best Practices" the NoVarsInStrings and Explicit options should be enabled? I believe the explict option has something to do with memory allocation but not really sure.
In a login script I am using I create $USERPROFILE the following way, $USERPROFILE = EXPANDENVIRONMENTVARS(READVALUE("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\@SID","ProfileImagePath"))
Using @SID within the variable makes this work great.
I also created the following variable, $MY_DOCUMENTS = EXPANDENVIRONMENTVARS(READVALUE("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "Personal"))
and use it quite often to verify cetain objects exist within the user's "My Document" folders.
Just trying to understand the logic. I figure I am missing something. Is there a better way to set these variables?
|
|
Top
|
|
|
|
#150494 - 2005-10-25 10:40 AM
Re: Question on Best Practices
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
I believe the explict option has something to do with memory allocation but not really sure.
Where did you get that idea? Explicit has nothing to do with memory. It is a flag to tell the interpreter that if it encounters an undeclared variable it should be treated as an error. By default when the interpreter encounters an undeclared variable it will simple create it and continue.
As one of the most common errors (and one of the hardest to track down) is spelling errors in variable names setting Explicit on means that you will catch these errors very quickly, which is why it is considered good practice.
Quote:
[RE: NoVarsInStrings] Just trying to understand the logic. I figure I am missing something. Is there a better way to set these variables?
I think you are just misunderstanding the intent of the command. It is not to avoid having variable values in strings, it is to avoid having variables in strings when what you want is the value.
Code:
; BAD! Variables in strings: $USERPROFILE = EXPANDENVIRONMENTVARS(READVALUE("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\@SID","ProfileImagePath")) ; GOOD! Variables not in strings: $USERPROFILE = EXPANDENVIRONMENTVARS(READVALUE("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"+@SID,"ProfileImagePath"))
There are a lot of good reasons why you should avoid variables in strings, but this simple example should do: Code:
"Value of variable $$FOO is '$FOO'" ? "Value of variable $$BAR is '"+$BAR+"'" ?
If you run this example you will see immediately why variables in strings can cause problems.
Quote:
I also created the following variable, $MY_DOCUMENTS = EXPANDENVIRONMENTVARS(READVALUE("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "Personal"))
This second piece of code is fine as the intention is that the variable name is in the string, not the value. This is so that the expansion of the variable value can be deferred, such as when you use the ExpandEnvironmentVars() function.
NoVarsInStrings and Explict are considered good practice because they will make your code more reliable.
If you ever intend to submit code to the UDF forum it is a posting requirement that the code must be able to work in with NoVarsInStrings and Explicit switched ON.
|
|
Top
|
|
|
|
#150495 - 2005-10-25 03:14 PM
Re: Question on Best Practices
|
rwright74
Lurker
Registered: 2005-04-10
Posts: 3
|
Les,
I know macros and variables are not the same thing. But in my strange and twisted world I like to think of them as being similar. I like to think of macros as the "Read Only" versions of a variable already set by someone else.
Quote:
and what does your last example have to do with any of it?
Just basically giving an example of a variable I use several time in my login script. I should have explained further. Anyways, here is an example of how I use it in my script.
Code:
IF EXIST("$MY_DOCUMENTS\My Company Documents\Drawings") = 0 MD "$MY_DOCUMENTS\My Company Documents\Drawings" ENDIF
Richard,
The whole memory allocation thing is a little long to explain. Basically, it was my understanding that by declaring a variable before setting it you were actually telling the interpreter to set aside or allocate resources or something like that.
Quote:
NoVarsInStrings and Explict are considered good practice because they will make your code more reliable.
This makes sense and I can see how it can screw things up or give unexpected results in some cases. I also read through the links provided by StarwarsKid and found some good information.
I am not opposed to conforming to standards. I just have never been that good at it.
Thanks for the help.
|
|
Top
|
|
|
|
#150496 - 2005-10-25 04:16 PM
Re: Question on Best Practices
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
The whole memory allocation thing is a little long to explain. Basically, it was my understanding that by declaring a variable before setting it you were actually telling the interpreter to set aside or allocate resources or something like that.
The "Explicit" option has no effect on memory. As discussed earlier it is simply a runtime switch to instruct the interpreter how to deal with undeclared variables when it encounters them.
However, having Explicit set means that you *do* have to declare all your variables at some point. You don't need to do it at the start of the code, but you do need to do it before you use the variable. You must also only declare the variable once per scope (except for ReDim).
This is a Good Thing 
As at some time during the script runtime you will need to declare your variable, either explicitly by DIMming it or implicitly by referencing it, there is no additional overhead to explicitly declaring it up-front.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1046 anonymous users online.
|
|
|