In some finite element code I wrote I had one big `state` type which I passed around to every function, which is basically the same as passing along the arguments, but only takes one argument :). Plus you don't have to play games with being careful about the types of globals if you type the fields of your state struct properly.
On Fri, Sep 19, 2014 at 9:58 PM, <vava...@uwaterloo.ca> wrote: > I'm now starting on my principal Julia project, which is a finite element > mesh generator. This is a large application divided into many smaller > functions. I face a commonplace programming style issue in this setting, > which is as follows. There are many large data items common to many of the > functions (tables about geometric entities, subdivisions and so on). In > order for the smaller functions to have access to these data items, there > are at least two solutions available in Julia (and in C and Fortran for that > matter): > > (1) Pass them in around as function arguments > > (2) Declare them as module-level global variables. > > The disadvantage of passing them as arguments is that function arguments > lists become depressingly long (and hence so do invocations) so code > readability suffers. > > The disadvantage of declaring them as globals is that it is hard to tell > which function is accessing and possibly changing which variable. Another > disadvantage is that the 'main' routine has to explicitly reinitialize them > when it starts and could create hard-to-catch bugs if it fails to > reinitialize one. There is also a possible performance hit in Julia for > doing this. (Could someone clarify that?) > > Furthermore, if I ever try parallelizing the code, this may influence > whether (1) or (2) is better. (Could anyone comment on that?) > > I'm wondering which approach is preferable in Julia. Perhaps there is a > third approach that beats both of these? > > Thanks, > Steve Vavasis >