Jump to content
  • Advertisement
Sign in to follow this  
maxest

Globals

This topic is 2013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Some time ago I worked on a (professional) project in which I was surprised to see quite a lot of globals. There were classes like CRender, CPhysics, CClient and so on, and there was file globals.h that tied together all these classes by declaring GRender, GPhysics, GClient. Now, the entire code relied upon these globals and often even a small change in gpu texture class header caused recompilation of a big part of the codebase (which was medium in size but the total recompilation time was around 1 min).

 

We've heard all these stories about globals being bad and personally I prefer to write code a different way but I must say that working with all these globals was very easy - where ever I was in the code I could always easily access the client's, render's and so on, properties.

 

So, what's your opinion?

Share this post


Link to post
Share on other sites
Advertisement

Instead of bare globals, I prefer the service locator pattern. This prevents recompiling a lot when making changes. But I personally prefer passing the objects via parameters, or on construction. Or, when for example all deratives of the class object need access to some object, create a protected method getSomeObject(). Simply using globals is not easy to maintain as you said, so I should avoid them where possible.

 

I agree however that easy access via for example globals can be nice to program with, that's why I sometimes use a service locator where it isn't necessary, but only for small projects.

Share this post


Link to post
Share on other sites

Didn't know the locator pattern before. Sounds like a nice idea although... in itself it needs a global variable :). Or be implemented as a singleton. But hey, can't we just implement CRender, CClient and CPhysics as singletons in the first place? Instead of declaring them global why not just have them declared in their respective files?

Share this post


Link to post
Share on other sites

Almost every time I am lazy and make something a global, I end up eventually needing to make it non-global.

 

I'm also not a big fan of the service locator pattern, though I do use it in places. It makes it hard to understand the dependencies, and you can only have one of a particular service.

 

Best to just declare/pass down dependencies explicitly in most cases.

Share this post


Link to post
Share on other sites

Didn't know the locator pattern before. Sounds like a nice idea although... in itself it needs a global variable :). Or be implemented as a singleton.

Or it can be not-evil and be neither of those.
If it's needed from program startup until shutdown, then make it a local variable inside main.

Globals are fine if you're writing a simple example, or if you really don't care at all about the long-term maintainability I the code. If the code is being rushed together to ship some product, and then will be thrown away, never to be touched again, then hacky globals are fine.
Otherwise, decades of software engineering practice and study has produced whole books on how to write software that is actually reusable and maintainable, and they all involve some kind of compartmentalization, or: not using globals! :P

Share this post


Link to post
Share on other sites


I must say that working with all these globals was very easy

 

That's kind of the problem in a nutshell though. Its so easy, you end up doing in all sorts of places that later on you'll wish you hadn't.

 

I like the fact that its hard to access my physics code from within my renderer, because it stops me cutting corners that will bite me later on.

Share this post


Link to post
Share on other sites

Multithreading with globals is also a very dangerous thing.

 

If all your data resides in isolated blobbs of object instances, you can be very sure that you can churn through each blobb in a seperate thread without any risk of race conditions. But if you use globals, you have that single data point that is accessed by all blobbs which prevents you from easily parallizing stuff.

Share this post


Link to post
Share on other sites

I don't think the ease of access is worth the messy code you'll wind up with when the project gets rolling. Granted it can be a pain when you have to go back and edit your constructors to pass in variables you forgot about earlier, but things make so much more sense when only having access to what you need.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!