Page 1 of 1 1
Topic Options
#150491 - 2005-10-24 09:55 PM Question on Best Practices
rwright74 Offline
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
#150492 - 2005-10-25 12:20 AM Re: Question on Best Practices
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Neither NoVarsInStrings nor Explicit have anything to do with using MacrosInStrings and what does your last example have to do with any of it?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#150493 - 2005-10-25 12:50 AM Re: Question on Best Practices
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
I asked this question before. Hope these help.

http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB1&Number=141277

http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=143844&an=0&page=0#143844
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#150494 - 2005-10-25 10:40 AM Re: Question on Best Practices
Richard H. Administrator Offline
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 Offline
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 Offline
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
#150497 - 2005-10-25 05:52 PM Re: Question on Best Practices
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
Has anyone ever mentioned that Richard H. is Totally Awesome?
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#150498 - 2005-10-25 06:03 PM Re: Question on Best Practices
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
I think it's been mentioned once or twice before.

FYI - the memory allocation thing isn't totally off base..

Many programming languages use variable declarations for memory resource allocation, especially since things like BOOLs require much less than STRINGS, and INTs and REALs fall somewhere in-between. Knowing what a var will contain in advance helps most compilers better allocate resources.

Since KiX uses VARIANTs for all var types (which can hold anything, but be "cast" to a specific format) there's likely no advantage to declaring vars in KiX in terms of memory allocation. As you've seen, and will likely encounter in your coding, there's a HUGE value in declaring them AND defining them to a known starting value, especially as the size of your projects increase.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 884 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.119 seconds in which 0.092 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org