[Closed] exit prematurely
Hi guys,
sorry for this stupid question but I am a little bit confused. Which ways are there to exit a function or handler prematurely without the return statement ? I read that one is supposed to avoid the return statement because of waste of memory or speed. So lets say I have the variables bError1 and bError2 upon which depends if the handler will be executed or not. Right now I would have to create an if statement around the whole handler so to check them there would be a lot of nesting going on (see code 2). Is there a more ergonomic way to do it ?
-- old style, not recommended ?
on ... pressed do
(if ( bError1 == false ) then
([indent]return false
)
if ( bError2 == false ) then
(return false
)
[/indent]-- main code
)
-- new style, too much nesting ?
on ... pressed do
(if ( bError1 != false ) then
(
[indent]if ( bError2 != false ) then
([indent]-- main code, nested )
[/indent][/indent])
)
Ergonomic way? No, economic? perhaps. I believe that portion of code that reads that would rather you exit a function instead “return <value>”, by just doing “<value>
I’d have to re-read it.
Hi,
AFAIK, the reason it should be avoided is because it’s relatively expensive in terms of cpu cycles.
So, my rule of thumb for return usage is: avoid when possible but us when necessary.
It’s especially important to avoid it when it’s going to be repeatedly evaluated, like in a case of a function that is repeated many times.
If you only need to break from the current code when it’s rarely evaluated (like in the case of a button pressed), I think it’s fine to use it. I also use it sometime to exit from the scope if it makes the code easier to read (instead of putting a long function inside an if statement), but I only use that when I know this scope will not be executed many times.
hOpe this helps,
o
Don’t get paranoid about the speed of HANDLERS. They are called once when you press a button. Even if they were calling a function to do something and that function would use return, you wouldn’t be able to tell the difference. The handler itself does NOT return a value, it performs operations upon a control action.
The speed warning applies to code that executes THOUSANDS or MILLIONS times, often within few seconds. The difference between a return statement in a function called to, say, evaluate each pixel of a bitmap and a function without return could be 20 vs 10 seconds or something like that. When you call a function one million times and waste a couple microseconds on each call, the wasted time can accumulate…
But worrying about the speed of an event handler is a bit out there…
Thanks, Ofer ! Thats exactly what I wanted to know. Bobo, thanks for stressing the speed issue. I was also referring to functions in my first sentence, not just handlers. But my code example was really bad, sorry ! The main reason for my question was more a matter of organization of code, rather than a matter of speed because if you have ten if-then-clauses wrapped around the main code to check for errors then it gets a little bit annoying. So from now on, I know that one can easily use returns, especially if a function is not called a lot of times. Thanks for clearing this up. Yeeeehaaaa !