Page 2 of 2 <12
Topic Options
#179644 - 2007-08-23 11:35 PM Re: Golf Code... [Re: Bryce]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
iif() doesn't make any sense in this simple task.
it hardly ever does.
_________________________
!

download KiXnet

Top
#179657 - 2007-08-24 11:20 AM Re: Golf Code... [Re: Lonkero]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Glenn
You can't put a UDF or most other functions in the test part.


Yes you can. Not to sure why you think that you cannot - do you have an example?

The tricky thing about IIf() is that BOTH the True/False sections are ALWAYS evaluated so you need to be very careful what you put there.

Here is a very simple example that illustrates a working scenario:
 Code:
$Var1=10
$Var2=5
 
$Action="Divide"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
$Action="Multiply"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
 
Function doMult($a,$b)
	$doMult=$a*$b
EndFunction
 
Function doDiv($a,$b)
	$doDiv=$a/$b
EndFunction


This works as you'd expect as even though both doMult() and doDiv() are called, the unused return value is simply discarded. This is dangerous though, as (in most languages) if $Var2 was zero you would get a "divide by zero" error even if the action was "Multiply". KiXtart is a bit more forgiving so you don't see the error.

Here is an example of when using functions causes an unexpected side effect:
 Code:
If 6=IIF($sType="New"
	,MessageBox("Thing does not exist - do you want to add it?","Add Item",4)
	,MessageBox("Thing already exists - do you want to change it?","Change Item",4)
	)
	"Ok, Thing is being actioned"+@CRLF
Else
	"Ok, you chose not to update Thing"+@CRLF
EndIf


If you run this then *both* MessageBox()'s will appear, though only the return value from the "Change Item" box will be used.

Top
#179661 - 2007-08-24 01:12 PM Re: Golf Code... [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
So this is an "undocumented feature" or do I misunderstand the manual?
Top
#179667 - 2007-08-24 01:42 PM Re: Golf Code... [Re: Witto]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You misunderstand, but maybe I didn't explain it very well \:\)

IIF only returns values. However, the values may be derived from any source including complex expressions, the return values of functions, object methods and so-on.

The problem is the way that the parser / runtime resolves the symbols. Basically the expressions in the IIF() statement are resolved to simple values *before* the conditional part is evaluated.

Take one of the examples:
 Code:
$Var1=10
$Var2=2
$Action="Multiply"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF


The IIf() will resolve something like this:
  1. Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))
  2. Iif("Multiply"="Multiply",doMult(10,2),doDiv(10,2))
  3. Iif("Multiply"="Multiply",20,5)
  4. Iif(1,20,5)
  5. 20


That's not necessarily the exact process, but it's close enough to see what the problem is. What is clear is that the evaluation of the conditional part is the last thing that happens.

This causes two problems.
First, both the true and false expressions have been evaluated - in this case both functions have been called.
Second, it means that you cannot use *any* expression which might be invalid even if it is not going to be used. This includes simple variables as well as more complex expressions.

Here is a good example of how easy it is to fall into the trap. Let's say that you want to assign a default when a value has not been passed on the command line:
 Code:
$=SetOption("Explicit","ON")
$=IIf(IsDeclared($SERVER),$SERVER,".")


This looks fine - if the user has passed "$SERVER" on the command line then it is used. If the user has not specified $SERVER then the local computer "." is used instead.

The problem is that $SERVER is evaluated before the conditional, so the script will abort with an "undefined variable" error if the user has not passed the variable on the command line.

IMO IIf() in KiXtart is an accident waiting to happen, and is only really useful for golf. My recommendation is to avoid it in production work.

Top
#179674 - 2007-08-24 02:31 PM Re: Golf Code... [Re: Richard H.]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
$var = "Yes"
IIf($var = "Yes", CallYess(), CallNo())
Function CallYess()
   
"Yes" ?
   
$CallYess = "True"
EndFunction
Function CallNo()
   
"No" ?
   
$CallNo = "False"
EndFunction

OK, I see.
AFAIK "If...Then...Else...EndIf" does not have this problem.

Top
#179677 - 2007-08-24 02:33 PM Re: Golf Code... [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Yes, you're right - this is what I was thinking of, but reversed my thoughts it as I wrote it.

Because both true and false functions are evaluated, it's easy to get into trouble with math functions - div/0 in particular.

Thanks for clearing that up!

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

Top
#179690 - 2007-08-24 10:26 PM Re: Golf Code... [Re: Glenn Barnas]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
You're just getting OLD there Glenn. As I recall you and I were some of the first to run into this issue. Got into conversation with Shawn and Jooel over it years ago as what looked like perfectly good code was giving the wrong response.

But true, unless it's very simple code you should probably avoid IIF in production.

Top
#179695 - 2007-08-25 12:55 AM Re: Golf Code... [Re: NTDOC]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
If there are any wondering what my final result was...

 Code:
If "A" = Split($array[$c],"-")[2]
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179707 - 2007-08-25 04:27 PM Re: Golf Code... [Re: Gargoyle]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
and without iif(), that's nice.
to really golf it, you would end up with:
 Code:
If Split($[$c],"-")[2] = A
_________________________
!

download KiXnet

Top
#179708 - 2007-08-25 04:36 PM Re: Golf Code... [Re: Lonkero]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
We could strip out the spaces and use single quotes too. :p
 Code:
If Split($[$c],'-')[2]=A
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#179709 - 2007-08-25 06:19 PM Re: Golf Code... [Re: Les]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
that wouldn't lower the golfscore though.
just would make it lonkenized, imo.
_________________________
!

download KiXnet

Top
#179711 - 2007-08-25 07:04 PM Re: Golf Code... [Re: Lonkero]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It was a joke... like the Dilbert cartoon of the PHB wanting a lighter laptop and being told to delete files to make it lighter.

I don't know why this version of UBB left out the tongue-in-cheek emoticon. :p Perhaps you missed that?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#179713 - 2007-08-25 07:57 PM Re: Golf Code... [Re: Les]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
indeed.

how did you know? you'r a psychic?
_________________________
!

download KiXnet

Top
Page 2 of 2 <12


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

Who's Online
0 registered and 837 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.086 seconds in which 0.066 seconds were spent on a total of 14 queries. Zlib compression enabled.

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