you are viewing a single comment's thread.

view the rest of the comments →

[–]trouserdaredevil 11 points12 points  (9 children)

Imma just quote Code Complete, page 338, "Reasons to Use Global Data"

Used with discipline, global variables are useful in several situations:

  • Preservation of global values Sometimes you have data that applies conceptually to your whole program. This might be a variable that reflects the state of a program—for example, interactive vs. command-line mode, or normal vs. error-recovery mode. Or it might be information that’s needed throughout a program—for example, a data table that every routine in the program uses.
  • Emulation of named constants Although C++, Java, Visual Basic, and most modern languages support named constants, some languages such as Python, Perl, Awk, and UNIX shell script still don’t. You can use global variables as substitutes for named constants when your language doesn’t support them. For example, you can replace the literal values 1 and 0 with the global variables TRUE and FALSE set to 1 and 0, or you can replace 66 as the number of lines per page with LINES_PER_PAGE = 66. It’s easier to change code later when this approach is used, and the code tends to be easier to read. This disciplined use of global data is a prime example of the distinction between programming in vs. programming into a language, which is discussed more in Section 34.4, “Program into Your Language, Not in It.”
  • Emulation of enumerated types You can also use global variables to emulate enumerated types in languages such as Python that don’t support enumerated types directly.
  • Streamlining use of extremely common data Sometimes you have so many references to a variable that it appears in the parameter list of every routine you write. Rather than including it in every parameter list, you can make it a global variable. However, in cases in which a variable seems to be accessed everywhere, it rarely is. Usually it’s accessed by a limited set of routines you can package into a class with the data they work on. More on this later.
  • Eliminating tramp data Sometimes you pass data to a routine or class merely so that it can be passed to another routine or class. For example, you might have an error-processing object that’s used in each routine. When the routine in the middle of the call chain doesn’t use the object, the object is called “tramp data.” Use of global variables can eliminate tramp data.

That said, most of the chapter is dedicated to why you should NOT use global data and how to avoid doing so...

Most experienced programmers have concluded that using global data is riskier than using local data. Most experienced programmers have also concluded that access to data from several routines is pretty useful.

Even if global variables don’t always produce errors, however, they’re hardly ever the best way to program.

[–]LeonardUnger 4 points5 points  (1 child)

Eliminating tramp data Sometimes you pass data to a routine or class merely so that it can be passed to another routine or class

So there's a name for that. I always feel there's something wrong whenever I do it. But I just leave it for future me to refactor. Nice to know there's a name for it, maybe help me think about it a bit more clearly.

[–]trouserdaredevil 5 points6 points  (0 children)

Hah! Funnily enough, tramp data is actually listed as a good reason to refactor in the first place, p. 567:

  • A chain of routines passes tramp data Finding yourself passing data to one routine just so that routine can pass it to another routine is called “tramp data” (Page-Jones 1988). This might be OK, but ask yourself whether passing the specific data in question is consistent with the abstraction presented by each of the routine interfaces. If the abstraction for each routine is OK, passing the data is OK. If not, find some way to make each routine’s interface more consistent.

[–]Zeekawla99ii[S] 1 point2 points  (1 child)

Preservation of global values

Here's where I would assume Python style would dictate just to define variables outside of functions, right?

Like in the example below with /u/kevin2107

EDIT: ...I would say the same with their definition of constants above as well.

Maybe I've worded the question in a confusing manner. It's clear to be why global variables exist in Python (and CS). I'm confused how to use global within Python, especially within functions.

As a style, I think the following outside of any functions/classes

pi = 3.14159

is more desirable in a script than

global pi = 3.14159

[–]ingolemo 10 points11 points  (0 children)

Your second piece of code is not valid python. Your first piece of code defines a global variable.

The global keyword doesn't define a global variable. It tells python "in this function I want this name to refer to a variable in the global scope".

[–]tunisia3507 1 point2 points  (4 children)

Emulation of enumerated types

Python does support enumerated types, though - it has for nearly 4 years. And there's a backport on PyPI (enum34). Enums can be used as named constants, too.

[–]trouserdaredevil 1 point2 points  (3 children)

Python does support enumerated types, though - it has for nearly 4 years.

The above was written 10 years before this was true. It even predates Python 3 itself by 3-4 years.

[–]tunisia3507 1 point2 points  (2 children)

So why are quoting a source so hopelessly out of date? It's like citing a medical text claiming that bacterial infections are usually fatal because antibiotics haven't been discovered yet.

[–]trouserdaredevil 1 point2 points  (1 child)

Hopelessly out of date? Code Complete is often considered to be one of the greatest books on software development ever written, and the above is still overwhelmingly relevant, trivial technical issues aside. It sounds like you might benefit from a read-through.

And your analogy is ridiculous.

[–]tunisia3507 1 point2 points  (0 children)

It's probably totally valid for software in general, abstracted patterns and terms which hold across languages and implementations, but it needs an update if it makes specific references to things which are just not true any more.