Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Lance

pros and cons of Global Variables and Local Variables?

This topic is 3380 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

Advertisement
pro: sharing is easy

con: anyone can change a global variable, so if something goes wrong with it, the bug could be anywhere in your code

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
con: anyone can change a global variable, so if something goes wrong with it, the bug could be anywhere in your code

And in any thread. Or threads.

Share this post


Link to post
Share on other sites
I typically stick all my variables inside a class and make them private so that only the class can change them. That way it is very easy to group and categorize the code in a logical manner.

If I create a global I'll usually make is constant so that no one can change it.

Share this post


Link to post
Share on other sites
Personally I prefer to never use global variables for the same reason that proper encapsulation of classes is so important.

Global variables make your code very hard to maintain and aren't necessary in even a mediocrely designed system.

Local variables on the other hand are used as required (within the scope of whatever function in which they are defined). Are you confusing them with member variables?

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!