C.6. Variables

C.6.1. Global variables

Do not use them without a very compelling reason because they tend to make code very difficult to understand. If used sparingly and judiciously in some circumstances however they can greatly simplify the code. Use your discretion.

C.6.2. Static Variables in Functions

Such functions are not re-entrant so do not use them at all if you'd like the code to work in multi-threaded contexts. Occasionally there is very strong reason to use them but avoid them otherwise.

C.6.3. Variable Declarations

Declare all variables at the top of each function. Declare one variable per line. It is easier to read if both datatypes and variables line up. This is tidy:

{
    ajint    a;
    ajint    b;
    ajlong   c;
...

whereas this is messy:

{
    ajint a,b;
    ajlong c;
...

This following code would conform to the latest C standard (C99) but is unclear (and C99 isn't supported by all the platforms on which EMBOSS should work):

{
.
.
    for (ajint a=0; a<b; a++)
.
.

The recommended layout is:

{
    ajint a;
    ajint b;

    ...

    for (a=0; a<b; a++)
.
.

C.6.4. Variable Initialisation

Always initialise object pointer variables to NULL. There are circumstances in EMBOSS where it can be risky not to (see Section 5.3, “Objects (C Data Structures)”). Initialise other datatypes as appropriate in the code. Align initialisation statements for ease of reading:

AjPStr    seqsubstring   = NULL;
AjPDouble xarray         = NULL;

Do not initialise declarations with functions. Never use for example:

AjPStr seqsubstring = ajStrNew();

C.6.5. Use of int, ajint, long and ajlong

To circumvent any problems arising from system-dependent sizes of variables (see Section 5.1, “Basic Datatypes”), EMBOSS assumes an ajint is typically 32 bits and an ajlong is typically 64 bits (see Section 5.1, “Basic Datatypes”). Use ajint, if 32 bits is enough, instead of int. Use ajlong instead of long or long long.

Match your datatype to what you need. If, for example, you are using an Alpha box then both your int and long variables will be 64 bits. In this case don't use only ajlong out of laziness as your code will run more slowly on other platforms.

Standard C int and long should be used in some circumstances, for example, as parameters to C system library functions.