Page 1 of 1

Best practices - Where to put update code

Posted: Wed Jan 18, 2017 11:56 pm
by dpatterson
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

Re: Best practices - Where to put update code

Posted: Thu Jan 19, 2017 4:01 am
by dunbarx
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

Re: Best practices - Where to put update code

Posted: Thu Jan 19, 2017 6:54 am
by dpatterson
Lots to think about.
LiveCode is very different from any of the other languages I've worked with.
Thanks for the insights.

Re: Best practices - Where to put update code

Posted: Thu Jan 19, 2017 1:00 pm
by Klaus
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

Re: Best practices - Where to put update code

Posted: Thu Jan 19, 2017 1:57 pm
by MaxV
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:

Re: Best practices - Where to put update code

Posted: Thu Jan 19, 2017 7:04 pm
by dpatterson
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.