Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foxpro NVL() behavior

Status
Not open for further replies.

quantzombie

Programmer
Aug 6, 2013
2
US
Hi All,

If I use nvl(var1,foo(xyz)) will the function foo executed everytime even if var is not NULL ?
 
Yes, and that of course are two furher "miracles".

It's easier to understand though. Even in compiled VFP ccode the single parts of the expression are readable by the runtime before it executes and combines the boolean results and before it does so, it can analize the logical expression itself and determine...
a) with OR it's sufficient, if the left side term returns .T. and
b) with AND it's sufficient the left side term returns .F.
...to not need to evaluate the secondary term.

In fact IIF as abbreviation for IF with the two branches "then" and "else" is even simpler to understand, because depending on the condition only one of the branches is executed. And the alternative for NVL with IIF of course also goes in the same direction. Well, in the end any function with special behavior could also do as such logical expressions or IIF, as VFP is a interpreted language, but NVL wasn't developed this way.

If they would change it, code using this behaviour would break, though.

Bye, Olaf.
 
Here's a nifty sample how you can swap two variable values, without using a third, making use of the NVL flaw. The only prerequisite is, the y variable is not .NULL., when swapping

Code:
x=1
y = 2
? x,y && 1,2
x = Nvl(y,_vfp.DoCmd("y=x"))
? x,y && 2,1

It's not really of relevance, as it may be faster to use a third helper variable anyway. Indeed y first is evaluated and stored in a temp memory before y=x is executed and finally x is set to the temp memoery variable, otherwise you'd expect both variables to end in the x value.

Bye, Olaf.
 
Hi,

From all that has been explained,tested and proven above

NVL(x, MESSAGEBOX("test")) does NOT behave the same as IIF(ISNULL(x),MessageBox("test"),x)

Both return the correct value but NVL evaluates the Messagebox function wheras IIF(...) DOES NOT

You may of course test with x = 1 and x = NULL

MarcK
 
Mark, you are correct in every particular.

I suggest that we have now answered the original question several times over, so perhaps we can drop the subject before we flog it to death.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
MarcK,

this is actually nothing new, we already said several times that both NVL and IIF return the value as documented, there is no erroneous return value, there is just the unwanted behaviour of NVL to evaluate the alternative value, even if unnecessary, while IIF offers an alternative to NVL, that does behave as wished. And therefore IIF was suggested, because it returns the value as NVL and saves time in comparison with NVL, because of not calling foo(), and this is what quantzombie wanted.

The topic is really covered well, and now you also have understood the difference. All is good and I agree with Mike, this thread is done.

Bye, Olaf.
 
Mike & Olaf,

You're right - the topic has been really well covered, meanwhile I also understood the behavior of NVL() and this thread is done.

But I think the discussion goes beyond the behavior of NVL(). In fact the question arises to me if we can rely on the explanations given by MS on how VFP commands/functions work or do we have to test each one thoroughly to check whether there aren't any undocumented/unwanted side effects. Even Hacker's Guide (my "Bible" [wink]) doesn't mention this behavior of NVL(), and I would have liked to know whether Tamar + Ted would have considered it a "feature" or a "bug".

Bye

M
 
OK,

the topic documentation may be worth a separate thread.

I can only give a little insight in that the german help was even less accurate than the english one. One reason for helps inaccuracy is the help writers are not the developers of the language and product itself, they are not techies. I'm sure they worked together with the VFP development team, but documentation is not tested and checked as thoroughly as the code itself.

Specifically about NVL, the help isn't wrong a single bit, it's just not complete, to tell you both parameters always are evaluated. In detail the NVL topic just makes the error to first tell you NVL returns a non-null value from two expressions, but it corrects itself in also telling, the return value of NVL can be null, if both expressions are null. And this was even not of any importance here and wasn't argued about at all. So the help is just not telling both expressions are evaluated, but that's not wrong, it's just a notice or warning missing. And as already said this isn't special at all. What is special is the optimisation of IIF and the optimisation of evaluation of logical terms overall. There is also a lot of rushmore that is not told, halfways secrets and halfways not told to be able to put the budget of the documentation to be more complete and balanced and an overall acceptable documentation. There is room for books about VFP and there are books, aren't there?

The help topic about IIF is more accurate and more of a shining example than the description of NVL, but where do you want to stop finishing a product and what should it cost?

In the end the help is written for developers, experts on programming, not for people trying to recode the language 100% compatible. Knowing known bugs and workarounds and knowing detail behaviour both from experience and own experiments is what is distinguishing the expert from the normal developer, technical user or end user. And there is giving back, it's not that experts keep their knowledge for themselves. You find many explanations, if you search for them.

If you want perfection, then go somewhere else, where perfection isn't really arguable, eg into nature, arts. Don't go for Foxpro, programming languages or computers in general.

Bye, Olaf.

 
Good question as to whether we would have "bugged" that behavior. If we had, we would have called it a "design bug." Clearly, it behaves as designed and documented, but is it a good design, given the way VFP handles expressions in IF, IIF() and DO CASE?

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top