Page 1 of 1

Better way to exit a plugin early

Posted: 21 Mar 2022 16:41
by jimlad68
I have this piece of code that lets my plugin finish early if the fhMessageBox "No" is clicked. It works fine but I was wondering if there was a simpler more elegant way to finish direct from the fhMessageBox. In my decades ago basic coding gotos and labels were a staple, but I get the impression that they are frowned upon by real programmers. I've had a look around and can find only 1 example of the fhMessageBox boxformat MB_YESNO, and none of MB_YESNOCANCEL, perhaps there are better methods of exiting a plugin early.

Code: Select all

etc
etc
--
strAnswer = fhMessageBox(" YES to complete action or NO to exit", "MB_YESNO")
if strAnswer == 'No' then
    fhMessageBox("exiting with no actions")
    goto done
end
etc
etc
 more routines
etc
    end
end
::done::

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 17:22
by tatewise
The 'structured' technique is to use if ... then ... else ... end or use the opposite condition or use error(...) function.

Code: Select all

if strAnswer == 'No' then
    fhMessageBox("exiting with no actions")
else
    -- do the action here
end
OR

Code: Select all

if strAnswer == 'Yes' then
    -- do the action here
end
OR

Code: Select all

if strAnswer == 'No' then
    error("exiting with no actions")
end
-- do the action here
BTW: To escape from loops the break command is often handy.

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 17:36
by Jane
Another option is simply to use return. assuming you are not down deep in sub functions.

Code: Select all

strAnswer = fhMessageBox(" YES to complete action or NO to exit", "MB_YESNO")
if strAnswer == 'No' then
    fhMessageBox("exiting with no actions")
    return
end
print('do stuff')

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 18:52
by jimlad68
Thanks for those suggestions.

- The error ended the plugin, but without a message?
- the if ... then ... else ... end worked but added an extra nest
- the return seems to be the simplest which I think I will keep,
- although I do like my goto.

I still wonder if there are any issues using goto?

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 19:04
by ColeValleyGirl
jimlad68 wrote:
21 Mar 2022 18:52

I still wonder if there are any issues using goto?
It won't work if FH5/6 because goto isn't supported in Lua 5.1, but that's unlikely to be a problem for you.

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 19:31
by Mark1834
I strongly prefer the technique Jane described, as it makes it fairly simple to end a script from any point by monitoring return values. It is also completely generic, and works just the same way in Lua, Python, VBA, C(++), and probably most other languages.

Too much if/then/else can make a script harder to follow for other than trivial cases, and while error handling is commonly used, the syntax tends to be more opaque and language (and even OS) dependent.

Re: Better way to exit a plugin early

Posted: 21 Mar 2022 22:22
by jimlad68
ColeValleyGirl wrote:
21 Mar 2022 19:04
It won't work if FH5/6 because goto isn't supported in Lua 5.1, but that's unlikely to be a problem for you.
That is interesting, I noticed when I was searching here
http://lua-users.org/wiki/GotoStatement
"A goto statement was added in Lua 5.2.0-beta-rc1 [6] [1] and refined in 5.2.0-beta-rc2"
and https://www.reddit.com/r/lua/comments/8 ... statement/
"The goto statement was added to enhance clarity in breaking out of nested loops, cleanly implement state machines and their close relative, interpreters.
Goto can be powerful in some very rare situations, but using it aggressively can lead to spaghetti code."

also interesting to show the controversy, but I did not understand much of it! https://news.ycombinator.com/item?id=18484221

Which made me wonder why lua did not have it before, and to thinking there was something wrong with goto if it was only introduced later.