M
Mark Jeffcoat
Chris Uppal said:I often read this, but I don't think there's an ounce of truth in it.
I confess, I'm a little surprised. I thought I was making
a claim on a similar level to "breathing is useful".
More accurately, I think:
There is a need (a perfectly genuine need) to be able to put data in some
"well-known" place where things can get at it. And without the data (or its
location) needing to know about the users in advance (so the data is decoupled
from its users, even though the users are coupled to the data -- but that's
Global variables are one way of providing that. And when used for that purpose
they work well. Of course, they can be misused to create "spaggetti data".
But that doesn't mean that they are bad when used correctly.
Okay. I certainly can't argue that it's possible to build
a correct and clear program that used globals to store that
data.
I see two potential problems:
First, it's not clear to me how I'm supposed to tell the
difference between globals done right, and "spaghetti data".
"Apply the intuition of an experienced programmer" probably
works quite well for you, but it doesn't seem particularly
helpful as advice to a novice. Would you care to take a stab
at offering a few guidelines on how to recognize and avoid
spaghetti data?
Second, I hate to lose the ability to analyze a method
entirely in terms of its parameters and the its object's
data (and thus have it easily testable). The way I solve
the problem you lay out above while keeping that ability
almost everywhere is to make sure that the classes that
implement the business logic of the program only use that
data in constructors and method calls; a method body never
has access to the global data store (which might be, as
you say, a DBMS).