payette.io

Nicolas Payette's website

17 Apr 2021

Against Globals

In my experience, a disproportionate amount of bugs in NetLogo models are caused by the mismanagement of global variables. Here are a few reasons why I think that is the case, in (roughly) decreasing order of importance.

There is a slippery slope at work here. Maybe at first you’re thinking: “It’s no big deal. I only have three globals and my model is so small that it’s easy to keep track of everything.” But models grow. (“What’s just one more global?”) Or they become the basis for multiple versions of the original model, where these globals are used in slightly different ways or sometimes not at all because it is easy to forget to remove an unused global. Minimizing the use of globals right from the start keeps us a step ahead in the fight against entropy.

If you google “global variables”, the first link that come up are the Wikipedia article (“They are usually considered bad practice precisely because of their non-locality”), CS101 lecture notes (“WARNING Global variables are not generally used. It is a contradiction of modularity to have variables be accessible in functions in which they are not used.”) and a page called Global Variables Are Bad. There also exists Wulf and Shaw (1973) Global variable considered harmful.

Still, sometimes, they are the right tool for the job. I would simply like NetLogo modelers to think of a global as something you “spend” rather than just declare for free. If, after careful consideration, you think that a particular situation warrants the complexity and the cognitive cost of using a global then, by all means, treat yourself to one!