Vivek Haldar: Global Variables

Vivek Haldar has been blogging up a storm with his video readings of seminal CS papers. I’ve written about a few of them but there’s lots more. See his blog for an up-to-date list. A recent post was about the paper Global variable considered harmful by Wulf and Shaw.

As suggested by the title, this paper has some connections with Djkistra’s famous paper Go To Statement Considered Harmful. The connections are much deeper than the shared title meme. Both papers argue that the construct they’re deprecating is harmful because it makes it difficult to understand the dynamic structure of a program.

The paper’s main argument is that variables should be scoped as locally as possible. When a function can have side effects—when it can modify variables that aren’t local to the function—the dynamic nature of the program becomes very difficult to understood. This is one of the principal arguments for functional programming. Wulf and Shaw go further, though, arguing for a stricter variable scoping that attempts to isolate the effects a variable change can have to as small an extent as possible. Take a look at Haldar’s video—or read the paper—for the details.

As with the admonition to avoid gotos, the idea that we should avoid global variables has become a sort of catechism that we recite but don’t understand. There are excellent reasons for the precept but if we don’t understand what they are, we can’t know when they don’t apply.

This entry was posted in General and tagged . Bookmark the permalink.