Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Negativecreep0

Using Externs?

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

Hey guys I have a question I handed in a vid game for one of my classes and used Externs heavily in C++. Now my teacher told me that this is frowned upon in the programming community. Is this true? I mean I could always make some class functions to get the values of the variables that I externed. But using extern is so much faster. Is it the wrong thing to do using externs

Share this post


Link to post
Share on other sites
Advertisement
Usually when you''re using externs, it means you have global variables, and your professor might be of the school of through that global variables are the devil. So it isn''t that he doesn''t like externs, it''s more along the lines that he doesn''t like global variables. It''s a hot topic, and I wouldn''t necessarily say it''s frowned upon in the programming community, unless he happened to interview every programmer on the face of the Earth.

Share this post


Link to post
Share on other sites
I think the point is, having global variables is ok as long as you only have a few. If you go overboard and make everything a global, you will be sorry. Instead consider making variables either static (local to a .cpp file), part of the unnamed namespace (Ditto but for C++), static class / struct members, or static locals.

That way they won''t pollute your namespace or be accessible outside of where they''re needed.

Mark

Share this post


Link to post
Share on other sites
The biggest problem with globals is that they can be modified from anywhere in the code. If you write all your code by yourself, you can get by with globals just fine since you know what each part of your program is doing, but if you''re in a team environment and your program has any complexity to it, you''re just setting yourself up for a disaster. It''s just good practice in general to avoid globals.

Share this post


Link to post
Share on other sites
Put more formally, the problem with global variables is ownership. Without clear ownership relationships, it becomes hard to guarantee the veracity of the global variable at any time. This is what the Singleton pattern was created to remedy. Unfortunately, the Singleton pattern has been abused so much that many people reject it reflexively.

With the current emphasis on (exclusive) object-oriented programming methodology, externs are a definite no-no. While I don''t agree with that, I do agree that "heavy" or "extensive" use of externs hints at refactoring and/or redesign opportunities.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!