简体   繁体   中英

Are Global Variables bad (in this situation)?

Well here I am sitting down for the millionth time telling myself "I'm going to write a game using SDL and C++" except this time I told myself I was going to write it in C. This is because SDL itself is written in C and it seems to just make sense to me. The game that I'm writing will be a simple "SHMUP" (Shoot 'em up) and it will probably be very short code-wise, so I'm wondering are a couple global variables bad? (like "struct base basevar;" or "int SCREEN_W" or something)?

Global variables aren't inherently "bad". However, they can easily lead to very bad code (in the sense of readability, testability, maintainability) if used carelessly. They can also lead to pernicious multi-threading issues (if you're using threads), again, if used carelessly.

Some people have a blanket rule that you should never use them. IMHO, this is too draconian, and if followed religiously, can lead to people passing around a pointer to a big everything_t to all functions, which isn't a great deal better (although even this simple approach makes your code re-entrant). However, please don't read this as advocation for "just go wild with globals!". They are the right answer only in a few situations.

In your example, it sounds like you have a bunch of settings/parameters that don't really change. Consider at least grouping them into a settings struct; this gives you the flexibility later to have eg a const settings *getSettings() function.

Well, let's consider why globals are considered bad. It's their unconstrained visibility. Global variables are accessible to all code, for reads and modifications. A design based on proliferation of globals, defeats structure. The reads and writes can come from anywhere. And while this may be OK when you first implement something (because you know the land), it can be catastrophic because of the kind of code it encourages. Soon you end up with a spaghetti of relationships and interactions.

Having said this, a blind hatred of globals is also not warranted. I mean, globalness can be relative. A class name is global within a namespace. A class member is global within the namespace established by the class. So, if the codebase for a class becomes large enough, the same argument made against globals, can likely be made against class members. In fact, namespaces and classes are, in part, mechanisms to contain this disease of globalness. They are (in part) mechanisms to draw boundaries of visibility and access.

So, in my view, globalness is not the issue. It's really about how it affects structure.

While probably not deadly in this situation, you don't know what you'll want to do with your code in the future. If the game gets bigger, global variable will make debugging hard. If you decide to re-use the code for another game or even for something drawing 2 screens at once, the globals will get in the way.

You should practice good programming style even when it doesn't matter so you'll get it right when it does.

It depends on whether or not you plan on re-using the code. If you ever plan on changing the code into anything else, ever, then do not use a global, because they're incredibly hard to factor out.

最好了解某些环境,特别是旧版本的Symbian不支持全局变量,甚至不支持静态局部变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM