Best practices - Where to put update code

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Best practices - Where to put update code

Post by dpatterson » Wed Jan 18, 2017 11:56 pm

I'm making progress on my first LiveCode project and have a question regarding best practices when it comes to where code should be located.

I currently have the following code:

Code: Select all

on mouseUp
   lock screen
   put empty into lineNo
   put the dgHiLitedLines of group selectComponentGrid into rowNumbers
   put the dgData of group selectComponentGrid into rows
   repeat for each item rowNumber in rowNumbers
      put rows[rowNumber] into row 
      log  row["id"] && row["sku"]
      dispatch "AddData" to group bomGrid of card inventoryItem of stack inventory with row
      if lineNo is empty then
         put the result into lineNo
      end if
   end repeat
   
   # Set focus to the first new row's quantity field.
   if lineNo is not empty then
      put "quantity" into colName
      dispatch "EditCellOfIndex" \
            to group bomGrid on card "inventoryItem" of stack "inventory" \
            with "quantity", lineNo 
      log "EditCellOfIndex result: " & the result
      put false into firstTime
   end if
   unlock screen
   close this stack
end mouseUp
This code lives in an OK button that is a sub-stack of my main stack and is shown as a modal dialog.
The sub-stack contains a DataGrid. The user select any number rows in the DataGrid and then clicks OK.
The code above adds a new row to a DataGrid on a card in the main stack for each row selected in the DataGrid in the sub-stack and then opens a field in first added row for editing.

My question is this:
Should the bulk of this logic be moved to the main stack's card and the OK button's script changed to just send a message to, or call a function of that card?

TIA,
Dave

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Best practices - Where to put update code

Post by dunbarx » Thu Jan 19, 2017 4:01 am

Hi.

I try to keep the number of cards and stacks to a minimum, unless really called for. That said, it is much a matter of style.

If your OK button does lots of stuff, and that stuff derives from clicking the button, then I would keep it in the button.

The reasons for card or stack script handlers (or beyond) is usually to place handlers that may be called from multiple places, functions and handlers that share a commonality of purpose, some shared context, anything that makes them "fit" better at a higher level in the hierarchy.

Jacque. :wink:

Anyway, those are general musings.

Craig

dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Re: Best practices - Where to put update code

Post by dpatterson » Thu Jan 19, 2017 6:54 am

Lots to think about.
LiveCode is very different from any of the other languages I've worked with.
Thanks for the insights.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Best practices - Where to put update code

Post by Klaus » Thu Jan 19, 2017 1:00 pm

Hi Dave,

since this is a question about the message path in Livecode, here a very good article about this topic, very good read:
http://fourthworld.com/embassy/articles ... _path.html


Best

Klaus

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Best practices - Where to put update code

Post by MaxV » Thu Jan 19, 2017 1:57 pm

Here my style:
  • Preparing all: "PreOpenStack" of the stack, so you don't need to lock
  • Preparing just a card: "PreOpenCard" of the card, so you don't need to lock
  • Common functions and handlers: Stack
  • To override common functions: card (for example 2 functions with the same name, the card function is executed, the stack none)
  • All the rest: where is handy for me :lol:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Re: Best practices - Where to put update code

Post by dpatterson » Thu Jan 19, 2017 7:04 pm

Klaus wrote:Hi Dave,
since this is a question about the message path in Livecode, here a very good article about this topic, very good read:
(URL prevented due to account restrictions)
Best

Klaus
Klaus, Great article. Thanks for the link.

Post Reply