Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:37:02+00:00 2026-06-07T04:37:02+00:00

After reading some SO threads on this topic: I’ve come up with these reasons

  • 0

After reading some SO threads on this topic: I’ve come up with these reasons for why global variables/singletons are bad.

  1. It becomes increasingly difficult to understand functions that the global state, as the code grows, more and more functions will modify that global state.
  2. It makes unit testing harder.
  3. It hides dependencies.
  4. You will have to rewrite code if someday it turns out that your global variable is not actually a singular object/variable.

I want to make a game in C++, and there will be a “heightmap object” that represents the landscape of the world in my game as heightmap. This heightmap can change. I want to use a global object for it. (I don’t expect to run into static initialization order issues, as there won’t be any other static variable that references this heightmap object).

Now, I know global state is bad, and global mutable state is even worse, for those above reasons. But it seems really, really cumbersome to do the alternative: Create a heightmap object at main() scope, and pass that heightmap object to every single function that wants to use it.

What if I’m 100% sure that there will only be one heightmap in my application? Also, since this is a small, solo project, I have faith that I will be able to understand what each function is doing to the global state? And I don’t see how the use of a global variable in this this case hurts unit testing. If I want to use a mock heightmap, couldn’t I just do globalHeightmap = generateMockHeightmap(); before calling the function that I want to test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:37:05+00:00Added an answer on June 7, 2026 at 4:37 am

    As certain as you may be about the characteristics of your project right now, I can pretty much assure you that at some point in the future you’ll need to modify the code that relies on this global variable. At that point the global variable will likely come back and make it harder for you to figure out the needed changes because state isn’t hidden (for example what if you accidentally change the state instead of reading from it – then the entire program is affected at some random point in the future). I can’t overstate how important it is for program maintenance and debugging to minimize state mutation points and global variables are pretty much the antithesis to that goal.

    Just overriding the global state map for your unit test seems fragile. What if you need to restore the old state, or mutate between states in the test? Then you wind up with a bunch of save/set/restore code.

    What if you ever want to add a threading model to your application? Using global state will make that transition that much more complicated.

    What if someone else helps you out with the project a year from now? Will they be able to understand the code? Will you be able to understand it a year from now (I know that I always try to write obvious code and add comments where it’s not specifically because I might be the person that comes back a year later and no longer remember a thing about the mechanism).

    Finally, if a non-global-variable approach seems like too much work or too complicated that probably means your alternate approach is too complicated or needs another idea/rework. There’s no reason that you can’t stash the heightmap into an object that’s created at the appropriate high-level game object and passed/stored in lower level objects as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading some material on this subject I'm still not sure what the difference
I am looking into using Lucene.NET after reading some bad reviews of SQL Server's
After reading some threads on misuses of exceptions (basically saying you don't want to
After reading some articles about XSS I have incorporated HTMLPurifier into my zend framework
Say I have a connection to a database. After reading some stuff I want
I came across couple of questions about OCL expressions. After reading some university slides
I'm trying to understand how ID3 tags work, so, after reading some documentation, I
After some reading and R&D, I have came to conclusion that ? Forms in
I am trying to get into android development, after some reading i have settled
I want to write a translator between two languages, and after some reading on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.