Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of the perils of globals in R and Stata

In recent conversations with fellow students, I have been advocating for avoiding globals except to store constants. This is a sort of typical applied statistics-type program where everyone writes their own code and project sizes are on the small side, so it can be hard for people to see the trouble caused by sloppy habits.

In talking about avoidance of globals, I'm focusing on the following reasons why globals might cause trouble, but I'd like to have some examples in R and/or Stata to go with the principles (and any other principles you might find important), and I'm having a hard time coming up with believable ones.

  • Non-locality: Globals make debugging harder because they make understanding the flow of code harder
  • Implicit coupling: Globals break the simplicity of functional programming by allowing complex interactions between distant segments of code
  • Namespace collisions: Common names (x, i, and so forth) get re-used, causing namespace collisions

A useful answer to this question would be a reproducible and self-contained code snippet in which globals cause a specific type of trouble, ideally with another code snippet in which the problem is corrected. I can generate the corrected solutions if necessary, so the example of the problem is more important.

Relevant links:

Global Variables are Bad

Are global variables bad?

like image 397
Ari B. Friedman Avatar asked Apr 02 '11 22:04

Ari B. Friedman


People also ask

What are global variables with examples?

Example of Global Variable in C You can notice that in line 4, x and y get declared as two of the global variables of the type int. Here, the variable x will get initialized automatically to 0. Then one can use variables like x and y inside any of the given functions.

What are the dangers of using global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

What are globals in Stata?

A macro in Stata begins with the word “global” or “local”. The command global tells Stata to store everything in the command line in its memory until you exit Stata. If you open another data set before exiting, the global macro will still be in memory.

How can we avoid using globals?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.


1 Answers

I also have the pleasure of teaching R to undergraduate students who have no experience with programming. The problem I found was that most examples of when globals are bad, are rather simplistic and don't really get the point across.

Instead, I try to illustrate the principle of least astonishment. I use examples where it is tricky to figure out what was going on. Here are some examples:

  1. I ask the class to write down what they think the final value of i will be:

    i = 10 for(i in 1:5)     i = i + 1 i 

    Some of the class guess correctly. Then I ask should you ever write code like this?

    In some sense i is a global variable that is being changed.

  2. What does the following piece of code return:

    x = 5:10 x[x=1] 

    The problem is what exactly do we mean by x

  3. Does the following function return a global or local variable:

     z = 0  f = function() {      if(runif(1) < 0.5)           z = 1      return(z)   } 

    Answer: both. Again discuss why this is bad.

like image 123
csgillespie Avatar answered Sep 25 '22 04:09

csgillespie