* Better way to exit a plugin early

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
jimlad68
Megastar
Posts: 911
Joined: 18 May 2014 21:01
Family Historian: V7
Location: Sheffield, Yorkshire, UK (but from Lancashire)
Contact:

Better way to exit a plugin early

Post by jimlad68 » 21 Mar 2022 16:41

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::
Jim Orrell - researching: see - but probably out of date https://gw.geneanet.org/jimlad68

User avatar
tatewise
Megastar
Posts: 27084
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Better way to exit a plugin early

Post by tatewise » 21 Mar 2022 17:22

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.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Jane
Site Admin
Posts: 8441
Joined: 01 Nov 2002 15:00
Family Historian: V7
Location: Somerset, England
Contact:

Re: Better way to exit a plugin early

Post by Jane » 21 Mar 2022 17:36

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')
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."

User avatar
jimlad68
Megastar
Posts: 911
Joined: 18 May 2014 21:01
Family Historian: V7
Location: Sheffield, Yorkshire, UK (but from Lancashire)
Contact:

Re: Better way to exit a plugin early

Post by jimlad68 » 21 Mar 2022 18:52

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?
Jim Orrell - researching: see - but probably out of date https://gw.geneanet.org/jimlad68

User avatar
ColeValleyGirl
Megastar
Posts: 4853
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: Better way to exit a plugin early

Post by ColeValleyGirl » 21 Mar 2022 19:04

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.

User avatar
Mark1834
Megastar
Posts: 2147
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Better way to exit a plugin early

Post by Mark1834 » 21 Mar 2022 19:31

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.
Mark Draper

User avatar
jimlad68
Megastar
Posts: 911
Joined: 18 May 2014 21:01
Family Historian: V7
Location: Sheffield, Yorkshire, UK (but from Lancashire)
Contact:

Re: Better way to exit a plugin early

Post by jimlad68 » 21 Mar 2022 22:22

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.
Jim Orrell - researching: see - but probably out of date https://gw.geneanet.org/jimlad68

Post Reply