Global Variables Are Everywhere
https://www.flickr.com/photos/fling93/31701072

Global Variables Are Everywhere


I have had this discussion several times now, and experienced a common misunderstanding of my argument, when I claim that someone has used a global variable inside a class. It's time to explain this once and for all.

A global variable, the way programming textbooks often explain it, is a variable defined at the top level of your program, and accessible to all units within. This is rightfully considered a Bad Thing(tm). When something is widely accessible, it becomes difficult to know what state it should be in when accessed next. The open access implies that you can never be sure who accessed it before, who will access it next, and in what ways. These questions are the Three Graces of Access (TGA).

Now, let's step outside the purely technical definition above, and look at a different case. A static variable in a class - i.e. a variable that is shared among all instances of that class. If the class is large enough, or variably used enough, then again, the answers to the TGA become the same as those for global variables: you can't answer. Therefore, despite not adhering to the textbook definition, that static variable is a global.

What about a singleton? A singleton is a class that can only create one instance of itself. Any attempt to create another instance simply returns the already existing instance. This is quite plainly a technical way to create a global variable without tripping the textbook definition. However, there are some cases when singletons are considered the best way to do things. For example, a log class that allows different units to write messages to a common log sink. In this case, we note that we don't actually care about the answers to the TGA, because it's clear that there are no side effects, that writing to the log does not effect state changes noticeable by other units.

So what defines a global variable is whether we can answer the TGA, and what governs its use is whether we care about the TGA. Once you understand that, you start seeing global variables in more places. One common occurence is in large classes with many members. These classes often have a name ending with "Manager". As the class grows and holds more and more members, the answers to the TGA become less and less clear. The classification of the classes members as global variables can therefore be fuzzy - at what point is clarity about the TGA low enough to merit classification as global variables? Well, clearly, at some point.

That's a good reason to keep an eye open and to occasionally refactor. It means that instead of applying dogma and textbook definitions, one needs to apply judgment. It also means that like a lot of other things that are "considered harmful", even globals have their uses, and we should learn what they are. But most of all, we should understand why some uses are legitimate and some not.

要查看或添加评论,请登录

Yosef Meller的更多文章

  • Earthquake lesson in Zagreb

    Earthquake lesson in Zagreb

    Take a look at the image above. Does anything strike you as special? It's a building that shows earthquake damage on…

    1 条评论
  • A Dragonfly Ornithopter?

    A Dragonfly Ornithopter?

    I enjoyed the new Dune (part 1) movie. But as a mechanical engineer, I had a problem with one of the central props of…

    3 条评论
  • The Agile Animal Farm

    The Agile Animal Farm

    Scrum is a software development ideology, where teams are supposed to be given autonomy to decide their own technical…

    2 条评论
  • In Defense of Code Comments

    In Defense of Code Comments

    Some things are not as obvious as they used to be. Was Columbus a hero or slave-lord? Is freedom of speech limitless?…

  • How to 3D-print this keychain

    How to 3D-print this keychain

    I've had some fun recently making this keychain for print on a Stratasys J750. That printer is the finest of Polyjet…

  • But is your software agile?

    But is your software agile?

    By now, almost all software organizations have at least heard of Agile software development, and mostly adopted at…

  • Engineers’ Age of the Cinema

    Engineers’ Age of the Cinema

    In the image above you can see a gear system, 3D-printed on a Stratasys J750. It rotates like any gear system, but it…

  • A Month Developing on Windows

    A Month Developing on Windows

    I've recently finished the first month (though including Jewish holidays) of employment in a place that develops on…

    7 条评论
  • The literacy gap in research programming - should we care?

    The literacy gap in research programming - should we care?

    Some things only reveal how deeply complicated they are when something goes wrong and exposes the inner workings. Take…

    2 条评论
  • A Gears-and-Pulleys Automated Driver

    A Gears-and-Pulleys Automated Driver

    Autonomous driving is considered the height of promise for artificial intelligence - deep learning, neural networks and…

    5 条评论

社区洞察

其他会员也浏览了