#138198 - 2005-04-20 06:44 PM
Re: General Question about Using Array Data and how to use their indicies
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Richard -- I guess I've never had a problem using vars in strings before which is why I don't see why everyone is so against it.
Aye, and there's the rub.
It's fine while it works - when it doesn't work its a real problem and it may take you a while to discover why your previously well behaved program has stopped working.
You don't need to set NoVarsInStrings. Just don't use vars in strings.
It's just bad coding practice. It doesn't mean your code won't work just as well as code which doesn't have vars in strings. However you are more likely to encounter a situation where it will fail, and even worse is that you are less likely to know about it.
You're probably fed up with examples by now, but here is a good example of code not working as you'd expect: Code:
If "$VAR" = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf If $VAR = "" "Variable $$VAR is not set " ? Else "Variable $$VAR is set" ? EndIf
The two conditionals are identical, except one has the variable in a string.
The output from this is: Code:
Variable $VAR is set Variable $VAR is not set
Now, that first result can't possibly be what the coder intended, can it?
|
Top
|
|
|
|
#138200 - 2005-04-20 07:48 PM
Re: General Question about Using Array Data and how to use their indicies
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
You don't have to use vars between quotes even when NoVarsInStrings is off do you?
No you don't have to.
Whether NoVarsInStrings is on or off, don't put vars in strings. You do need to be aware of the setting of NoVarsInStrings if you are using the "$" character in a string as a real character rather than as a variable metacharacter.
Quote:
I mean, in your first example (if you can even do it), why would you use quotes when you don't need it?
I did do it. That's real code and real results cut'n'pasted from my own console.
The example illustrates the mechanism by which even simple code can get screwed up, I'm sure you can extrapolate the example to some more realistic scenarios.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 557 anonymous users online.
|
|
|