this post was submitted on 06 Jul 2025
18 points (100.0% liked)

programming

258 readers
3 users here now

  1. Post about programming, interesting repos, learning to program, etc. Let's try to keep free software posts in the c/libre comm unless the post is about the programming/is to the repo.

  2. Do not doxx yourself by posting a repo that is yours and in any way leads to your personally identifying information. Use reports if necessary to alert mods to a potential doxxing.

  3. Be kind, keep struggle sessions focused on the topic of programming.

founded 2 years ago
MODERATORS
 

If they didn’t exist in a language would you be upset? Any obvious things that can’t be done without global variables? I don’t think there is, but I could have missed something. I know that Haskell doesn’t have them and I think some newer ones like V also don’t have them.

you are viewing a single comment's thread
view the rest of the comments
[–] BeamBrain@hexbear.net 6 points 2 weeks ago

Definitely ask yourself whether they really need to be, but using a global variable makes sense if it's something that's going to be used by a large number of different parts of the program.

For example, the game I'm developing uses a variable called control_mode to determine how to handle the player's button presses. Anything that can be affected by those button presses (the player avatar, the menus, etc.) needs to be able to know the game's current control mode to be able to handle user input, so e.g. the player character shouldn't be able to move during cutscenes or when the game is paused. Since so many different parts of the program need to read control_mode, I made it global, but I only change it from a dedicated control manager.