Skip to content

Instantly share code, notes, and snippets.

View alee's full-sized avatar

Allen Lee alee

View GitHub Profile
@alee
alee / against-globals.md
Created June 15, 2018 23:04 — forked from nicolaspayette/against-globals.md
Why you should probably avoid global variables in NetLogo

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:

  • Code is not just a way of getting the computer to do what you want it to do—it is a way of conveying meaning to the reader of your code. When you declare a global variable, the meaning you convey is: “Hey! This thing is important throughout the whole program. You should always be paying attention to it.”

    Sometimes it’s justified , but very often, it is not. An unjustified global is not only failing to convey the right message: it’s actively misleading.

When I approach a model’s code for the first time, the first thing I do is look at the declarations on top: what breeds do we have, what agent variables do we have, and what global variables do we have? That gives me the “big picture” and tells me what I should be paying attention to when trying to understand what the code is do