Director: notes on optimizing If logic
I learned to program on the streets, in the school of hard knocks, instead of some sissypants classroom with “rules” and “techniques” and “sound, solid advice”. So occasionally frequently I learn new things about programming that I perhaps should have learned long ago. This may be one of those things; a bit o’ wisdom that is actually common knowledge. But I’ll share it here as a gift to my fellow hard knockers. Wait, that sounded wrong …
if a=1 then
if b=1 then
put "it worked"
end if
end if
becomes this:
If (a=1) and (b=1) then
put "it worked"
end if
But I discovered a potential dowside with this the other day. I put together a little test to understand how and works in Director. This is what I coded up:
on startmovie
if (testfunction(1) = true) and (testfunction(2) = true) then
put "it worked"
end if
end startmovie
on testfunction(iInput)
put "testing " && iInput
return false
end
Notice that testfunction always returns false. When startmovie is called, the message window displays this:
-- "testing 1" -- "testing 2"The results above show that even though testfunction(1) was called and returned false, Director still went ahead and called testfunction(2). If testfunction happened to be a CPU intensive routine, we would have needlessly called it a second time. So in this case, rolling two seperate If statements into one actually deoptimized my code.
Of course, what actually is best depends on the circumstances. If testfunction returned false only once in a blue moon, combining the If statements together using and may still the way to go. Now that I understand this I’m going to think twice before I roll up my if statements.


I'm Hanford Lemoore. My parking skills are unparalleled.





