Earlier quoted context omitted.
You get functions that are easily testable in isolation with all state provided in parameters. You also get explicit dependencies and scoping controlled by caller. I don't mind globals but saying you get nothing for avoiding them is :/
If that’s so useful, make your language support the concept of lexical environments instead. Otherwise it’s just manual sunsetting every day of week. Our craft is full of this “let’s pretend we’re in a lisp with good syntax” where half of it is missing, but fine, we’ll simulate it by hand. Dirt and sticks engineering. (To be clear, I’m just tangentially ranting about the state of things in general, might as well post…
Global variables are not the problem
91–100 of 165 posts
Re: Global variables are not the problem
#92Any program that uses a database has a very similar problem to global variables. As Gilad Bracha has pointed out, types are antimodular, and your database schema can be considered one giant type that pervades your program, just like globals can be. I don't think we have tools to compositionally solve this, across different programming languages.
Re: Global variables are not the problem
#93The one where the author almost rediscovers the singleton pattern.
Re: Global variables are not the problem
#94Earlier quoted context omitted.
If that’s so useful, make your language support the concept of lexical environments instead. Otherwise it’s just manual sunsetting every day of week. Our craft is full of this “let’s pretend we’re in a lisp with good syntax” where half of it is missing, but fine, we’ll simulate it by hand. Dirt and sticks engineering. (To be clear, I’m just tangentially ranting about the state of things in general, might as well post…
So, making it implicit again? No.
Re: Global variables are not the problem
#95Re: Global variables are not the problem
#96Any program that uses a database has a very similar problem to global variables. As Gilad Bracha has pointed out, types are antimodular, and your database schema can be considered one giant type that pervades your program, just like globals can be. I don't think we have tools to compositionally solve this, across different programming languages.
Re: Global variables are not the problem
#97SQL databases are global variables
Re: Global variables are not the problem
#98SQL databases are global variables
And man, the bugs they cause.
SQL or 15000 lines of C++ or Go or Rust doing whatever with files.
Re: Global variables are not the problem
#99From the article> "Static Function Variable: In C inside a function, you can declare a static variable. You could consider this as a local variable but I don't since it's on the heap, and can be returned and modified outside of the functions. I absolutely avoid this except as a counter whose address I never return." These variables are not on the heap. They are statically allocated. Their visibility is the only thing…
In C++, there is a difference: function-static variables get initialized when control first passes into the function. That difference matters in cases where the initial value (partly) comes from a function call or from another global. For example, in C++ one can do:
int bar = -1;
void foo() {
static char * baz = malloc(bar);
…
}
void quux() {
bar = 13;
}
That’s fine as long as the code calls quux before ever calling foo.They added that feature not to support such monstrosities, but because they wanted to support having static variables of class type.
If they were to design that today, I think they would require the initialization expression to be constexpr.
Re: Global variables are not the problem
#100Please no. Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.