Obligatory, mutable global variables are evil.
The definition of a variable is that it’s mutable. If it’s immutable it’s constant.
There’s no ISO standardized definition for variable. People use that word with all kinds of meaning.
This is needlessly obtuse. The definition of the word is that it’s non-constant. There isn’t an ISO definition of the word no, but there are many reputable dictionaries out there that will serve as an alternative.
Waaaait a minute… isn’t it called a variable because the contents are, you know, variable?
I feel like it’s like pointers.
“Variable” refers to the label, i.e. a box that can contain anything (like *ptr is a pointer to [something we dont know anything about])
Immutable describes the contents, i.e. the stuff in the box cant change. (like int* ptr describes that the pointer points to an int)
Rust makes it very obvious that there’s a difference between constants and immutable variables, mainly because constants must be compile time constants.
What do you call it when a variable cant change after its definition, but isnt guaranteed to be the same on each function call? (E.g. x is an array that’s passed in, and we’re just checking if element y exists)
It’s not a constant, the contents of that label are “changing”, but the label’s contents cant be modified inside the scope of that function. So it’s a variable, but immutable.
And more generally mutable aliasing references of any sort are evil. Doesn’t mean they’re not useful, just that you need magic protection spells (mutexes, semaphores, fancy lock-free algorithms, atomics, etc) to use them safely. Skip the spell or use she wrong one, and the demon escapes and destroys all you hold dear.
I’ve once had a course involving programming and the lecturer rewrote the code, which we were usually using at our institute, making ALL variables global. - Yes, also each and every loop counter and iterator. 🤪
There’s no way you teach a uni course and do this kind of thing unless to demonstrate poor practice/run time difference. Are you sure you were paying attention?
Yes. He really thought it was efficient and would avoid errors if literally all variables were defined in a single Matlab function he called at the beginning of the script. We students all thought: “Man, are you serious?” As we didn’t want to debug such a mess, in our code, we ignored what he was doing and kept using local variables.
Ah I misread I thought it was specifically a programming course. I can expect this from a math prof.
Lecturers at universities tend to have little to no industry experience at all.
Is it really tempting for people? They’ve given me too many headaches when I’ve had to reformat or add functionality to files.
Unless it’s a simple single use script that fit on the computer screen, I don’t feel like global variables would ever be tempting, unless it’s for constants.
Most people suck at software engineering.
Plus, there’s always the temptation to do it the shitty way and “fix it later” (which never happens).
You pay your technical debt. One way or another.
It’s way worse than any gangster.
amen
Plus, there’s always the temptation to do it the shitty way and “fix it later”
double amen
Rarely have I ever actually had consequences for my sins, which tends to be why I don’t go back and fix them…
If tech debt weight is felt in any way, it tends to get fixed. If it’s not felt, it’s just incredibly easy to forget and disregard.
(This is mostly me not learning my lesson well enough from my time being on Tech Debt: The Team. I do try and figure out the correct way to do things, but at the end of the day, I get paid to do what the boss wants as cheaply as possible, not what’s right :/ money dgaf about best practices until someone gets sued for malpractice, but on that logic, maybe the tech debt piper just hasn’t returned for payment from me yet… Only time will tell)
For me most of the people who have written our most annoying tech debt left the company long time ago.
Then, at your new job, you see garbage code and wonder what dumbass would put global variables everywhere
They’ve given me too many headaches…
I.e. you did use them, but learned the hard way why you shouldn’t.
Very likely OP is a student, or entry-level programmer, and is avoiding them because they were told to, and just haven’t done enough refactoring & debugging or worked on large enough code bases to ‘get’ it yet.
Is it really tempting for people? They’ve given me too many headaches when I’ve had to reformat or add functionality to files.
I don’t get it either. Why would you ever feel the need for them to begin with?
Unironically: For in-house scripts and toolboxes where I want to set stuff like input directory, output directory etc. for the whole toolbox, and then just run the scripts. There are other easy solutions of course, but this makes it really quick and easy to just run the scripts when I need to.
This community makes more sense when you realize the majority of users are CS students.
Depends on what you’re doing. Functional programming has its own downsides, especially once you want to write interactive programs, which often depend on global states. Then you either have to rely on atoms, which defeat the purpose of the functional programming, or pass around the program state, which is janly and can be slow.
I personally go multi paradigm. Simpler stuffs are almost functional (did not opt for consting everything due to performance issues), GUI stuff is OOP, etc.
Well, if you’re writing something the user will be looking at and clicking on, you will probably want to have some sort of state management that is global.
Or if you’re writing something that seems really simple and it’s own thing at first but then SURPRISE it is part of the system and a bunch of other programmers have incorporated it into their stuff and the business analyst is inquiring if you could make it configurable and also add a bunch of functionality.
I also had to work with a system where configurations for user space were done as libraries setting global constants. And then we changed it so everything had to be hastily redone so that suddenly every client didn’t have the same config.
This, but with not making unit tests instead of global variables.