Sharing Reusable code - guidance

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kunicki
Posts: 12
Joined: Wed Mar 10, 2010 2:28 pm

Sharing Reusable code - guidance

Post by kunicki » Mon May 09, 2011 4:05 pm

I am looking for advice on application design with the goal of code reuse. I am working on a project that will produce a few reusable libraries that I will want to use in other projects. For example one library is for generic database access while another library is for web service access.

In other environments, I would probably split these two libraries out into code modules. Then in each project that I want to use these libraries I just make a link to the reusable code modules wherever they reside. (Some shared location). It seems that the LiveCode idea is to import these modules (or standalone stacks) into your Main stack. However, then I have to worry about keeping the projects up to date with code fixes.

What is the best way to accomplish code sharing between projects?

Example 1: Save each module as its own stack and then load the stack realtime? If I do this, what happens when I compile and need to distribute the project.

Example 2: Is there some way to reference these external stacks with my resuable libraries in the mainstack so that when they are compiled into an EXE, they are merged together into the application?

Thanks for any suggestions and guidance you can offer.

K

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9679
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sharing Reusable code - guidance

Post by dunbarx » Mon May 09, 2011 4:59 pm

K.

The letter rings a bell...

Craig

kunicki
Posts: 12
Joined: Wed Mar 10, 2010 2:28 pm

Re: Sharing Reusable code - guidance

Post by kunicki » Wed May 11, 2011 3:34 pm

Thanks for the ... vague reply. Any guidance on this is appreciated. This is more about good design and maintenance than it is purely technical.

Any takers, any advise?

Klaus
Posts: 13837
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sharing Reusable code - guidance

Post by Klaus » Wed May 11, 2011 4:34 pm

kunicki wrote:Thanks for the ... vague reply.
:D

Hi K,

I would go with example 1!

1. Keep your library stacks anywhere on your harddrive
2. Add a script to your mainstack(s) that need to use the libs like this:

Code: Select all

on preopenstack
  if the environment = "development" then
    put  "path/to/your library stack/on your harddisk.livecode" into tLib
 else
   put the filename of this stack into tLib
   ## Create the new necessary path to your lib

   set itemdel to "/"
   delete item -1 of tSttLibackPath
   put "/Libs/myLib1.livecode" after tLib
   ## I used a subfolder "Libs" in my example.
 end if

  ## Now you can:
  start using stack tLib

  ## more preopenstack stuff...
end preopenstack
3. then use the standalone builder to copy your lib(s) into the standalone folder.

Get the picture?


Best

Klaus

kunicki
Posts: 12
Joined: Wed Mar 10, 2010 2:28 pm

Re: Sharing Reusable code - guidance

Post by kunicki » Fri May 13, 2011 7:43 am

Klaus,

Thank you for this guidance and it is helpful. I am experimenting with this suggestion and it seems logical. Just a followup question:

Does it seem that this concept would also work equally well for mobile development, I assume that when I create my standalone application that all the modules are put together and I just have to load them dynamically from the mainstack using the same directory where the mainstack sits?

Thanks,
K

Klaus
Posts: 13837
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sharing Reusable code - guidance

Post by Klaus » Fri May 13, 2011 12:30 pm

Hi K,
kunicki wrote:... I assume that when I create my standalone application that all the modules are put together and I just have to load them dynamically from the mainstack using the same directory where the mainstack sits?
I'm not sure to understand?!
What dou you mean "...all the modules are put togehter..."?
You can use the standalone builder settings to copy all your lib stacks to the final app bundle.

On iOS you can query:
specialfolderpath("engine")
to get the folder where your standalone and all copied resources/stacks/subfolders are in.

Is that what you mean?


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7242
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sharing Reusable code - guidance

Post by jacque » Fri May 13, 2011 7:18 pm

Basically, the work flow would be to "start using" your library stack(s) during development. That way you only need to keep a single library updated and all your development stacks will use the latest one dynamically.

When it is time to build a standalone, use the Stacks pane in the Standalone Settings dialog to add your library stack as an inclusion. When you build, the library stack will be copied to the standalone. It's pretty painless.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

kunicki
Posts: 12
Joined: Wed Mar 10, 2010 2:28 pm

Re: Sharing Reusable code - guidance

Post by kunicki » Mon May 16, 2011 10:04 am

Thank you all,

You have helped with my question. Sorry for mixing words. I am still getting use to the idea of stacks (as opposed to code modules). So sorry for mixing up the language.

This is fascinating! Every language has their means for handling the dev environment and production and it is nice, the LiveCode way is very straight forward, though it wasn't self-evident from the beginning.

Thanks again,
K

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: Sharing Reusable code - guidance

Post by QuangNgo » Fri Sep 23, 2011 8:36 am

Hi Klaus,

Your code was very helpful for me. However this was only used for one livecode file.

Code: Select all

put "/Libs/myLib1.livecode" after tLib
              start using stack tLib
     


In my case, I use the modules folder to contains all modules and It might have more than one livecode files.
I can use repeat statement to get all files in modules folder and start using them as you mention before. But I thought that is not the good solutions
Could you please give me some solutions how I can use all files in that modules folder ?

Thank you so much
Regards,
Quang

Klaus
Posts: 13837
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sharing Reusable code - guidance

Post by Klaus » Fri Sep 23, 2011 1:18 pm

Hi Quang,

there are two solutions to your problem:
1. put ALL scripts of your modules into ONE stack.
but maybe this is not possible, so you have to load all modules! see 2.

2. in a standalone you can load up to 50 (FIFTY!) stacks as a library!
So as long as the number of your modules <= 50 you can load all modules in a repeat loop.

Here is a little script that will load ALL available modules automatically, means you do
not have to type all the names of the modules, simply load all existing modules in that folder :D

Code: Select all

...
put "/Libs/" after tLibFolder

## Store current DEFAULTFOLDER and restore it later, this is good programming style! ;-)
put the folder into tOldFolder
set the folder to tLibFolder

## Get all files in that folder
put the files into tFiles

## This is not necessary if you only have LiveCode files in that folder:
filter tFiles with "*.livecode"

## Now load all libs:
repeat for each line tLib in tFiles
  put tLibFolder & tLib into tLibraryStack
  start using tLibraryStack
end repeat

## Good style :-)
set the folder to tOldFolder
...
Best

Klaus

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: Sharing Reusable code - guidance

Post by QuangNgo » Sat Sep 24, 2011 2:44 am

Hi Klaus,

Thank you so much that was exactly I want ^^

Quang

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”