Modules – Design and Develop Reusable Code

Till Module is Till program that has header and functions sections. If a module is the running module, it will be called the “main module”, or simply “the module” or “the program”.

A module can use other modules. The module will be called “the calling module”, and the other modules will be called “the called modules”. When a module calls another module, all of the called module variables and functions can be used by it. Duplicate object keys are forbidden. All of this modules connectivity results in a modules tree that is resolved by Till system.

Calling a module is done by appending the module name, relative path or full path to system call “#mod”. The extension “.till” is not mandatory in the name or path, and can be omitted.

Here is an example of a program and three modules. To test the example, put each of the following codes in a separate file (file names are in the first row of each code), and run the program (main module):

The result of running “app.till”:

There are two main reasons for working with modules: modularity and reusability. If there are sections of code that represent cohesive coding spheres, than it makes sense to put these codes in separate programs and relate to them from the running program. Moreover, if there are objects that are used in more than one program it is useful to assemble these objects into a module that will be used by programs.

Global Modules

If objects are to be called anywhere then they should be called regardless of their location. These globally accessed modules are called “global modules” and are created by using the Till Launcher to convert a module to global module. Thence, that global module will be accessed by its name only.

In case it is needed to manage Till global modules, Till provides the Till Global Modules Manager, which is a Till executable program “gmod.exe”, also referred as gmod. With gmod you can add modules to Till global modules repository, access the modules, edit or remove a module, and even clear the repository.

Say we have program “util.till” that is to be used globally:

* all global variables and functions are in boldface

For the program to be used globally it should be able to be compiled by its own, and therefore, well defined. So, functions that have parameters that are to be used in programmatic structures like looping and conditional, should be relayed to local defined variables, that will be used instead. This modification is demonstrated in functions “sum” and “average” where parameter “list” was relayed (assigned) to local predefined list variable “_list”.

Moreover, all function parameters except from well-defined list parameters, are regarded undefined parameters in the context of global modules, and care should be taken to avoid ambiguity.

To make the program global, open it with Till Launcher, and click “GLOBAL MODULE”. If the program is compiled without errors, it will be available globally under its filename (file extension is not mandatory). Note that when a module is called by its name (without structural path) it is firstly searched in Till global modules and then in the current directory, and therefore it is recommended to give it a name that will not coincide with other programs.

Example of an application “app.till” that uses (calls) global module “util”:

* all global variables and functions from global module “util” are in boldface