Variable containers

A few month ago I have written about modularisation and the importance of programming in science. To get a little bit deeper into the topic, I will explain some further concepts in programming. One of them, which is especially important in modularised programs, are variable containers. The basic idea is that all variables, which are exchanged between the modules, are stored in a own module, which is then included into every other module. Nevertheless, some precautions have to be taken to make this concept a success in larger programs.

Module plan of my diploma thesis

Module plan of my diploma thesis. Variable container can be seen in the upper left as CR module.

I personally started to use them when I worked during my diploma thesis on the implimentation of a simple climate model. The idea was to have a flexible, and as a consequence, modularised model code, to adapt it to different forms of applications. Since the model was written in Fortran all the typical progamming concepts fell apart. Nevertheless, I tried my best to use some ideas from object orientated programming to achieve my goals. Unfortuneately, the OOP functionalities of Fortran are limited, even in the F95 version of the language. So a compromise was needed, without getting lost in writing thousands of get and set routines.

The answer to it was the variable container, which is relatively simple to handle. All variables in it are available in all other modules, and any change to one of the variables is seen by the other modules, when they are called. Helpful is to assign each variable to a parent module and make this visible with the help of naming conventions. By this it is assured that each variable is unique in each module and can therefore not be cofused or overwritten by local ones.

But what are the advantages? The most important advantage is during documentation stages, since all important vairables can  be found in one location, together with their initial initialisation. Commenting their usage at this stage helps anyoine to reuse the code. Furthermore, when it is used for all main variables it offers the potential to restore with a known state of the variable container the whole program. This might be useful when models are written and should be restarted or when different states are predefined for ensemble simulations.

Disadvantages can be seen when programming langauges have hard limits on variable or module sizes. In fortran it can be in  the simplest way implemented with a seperate module, but these have limits in sizes (at least I run into trouble during my diploma). On the script language site, like matlab or R, it is possible to use structs or lists to implement it. One function is used to set them up and afterwards this variable is given over to each other function in each module. This concept proves therein very effective, because saving and loading of just one overall variable is quite simple.

All in all, in environments of functional programming, variable containers are very effective and convinient. It helps to structure a code, to document it and allows many flexibilities in designing different form of programs.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s