Globals are still considered harmful to your health
Backed up from a local Blogger export (8911595847890050661/8911595847890050661.html) on 2026-01-01.
Sometimes there are well defined technical reasons for this. Some people detest them because they have been told they are bad. Other more thoughtful people understand some of the serious problems that globals have. Many times in rapid prototyping (ala beatbox) or in general game dev people get a bit too global-happy and it can be a serious problem… I’ll list a few of my main reasons for really not liking them:
- Globals can hide bugs, if you don’t name them intelligently they can be overriden by locally scoped variables.
- Globals don’t have well defined access patterns, anything in any thread can access them. This can make porting a single-threaded app to multi-core a super fun hairpulling exercise.
- Globals enable sloppy coding and can ruin layering.
- Globally defined classes don’t have deterministic initialization order, you better not count on one class being ready before another one. Go for two-phase construction if you need a global class.
It’s not all bad though:
- Having your root system(s) as objects at global scope simplifies your life.
- Pre/Static allocation is your friend. Many applications can be designed to require vary little dynamic allocation and this can really improve speed.
- Sometimes when you’re maintaining code it is less intrusive to add a global or static method then touch every method on down the call stack.
They’re there for a reason. What’s yours?