Jump to content
  • Advertisement

Archived

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

sprinter_trueno

Is 'extern' evil ?

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

Hi ppl, I have this problem of dealing with a module (just using C) that is way too big. This module in itself is definitely not overdesigned but has simply alot of functionality. My questions are: 1. Should I, for encapsulations sake stay with it like it is or, 2. Should I split the module in terms of readability ? The problem I have with splitting is that almost all the functions depend on the modules data. If I start to cut out functions then those still need access to the data of the original module or some other configuration data within the same. How is this done properly ? The only solution I come up with, is putting all configuration (defines,typedefs,..) in a seperate file which is included by the original and the new split files and give the split files direct access through the "evil" ''extern'' keyword to the needed data. Even better would be to declare all exteral access right in the additional configuration file which will be included by all the others. Frankly speaking, since the split files belong together, the use of ''extern'' does not break encapsulation of a module that happens to consist out of just more than one file, but I am simply not sure about this. Any comments appreciated. Lorenz

Share this post


Link to post
Share on other sites
Advertisement
extern is not evil, use what works best for you; there is no need to stay away from certain things because they are ''evil''.
The best way would be to split up the module into several source files and a single header. Then use something like this:

/* In header */
#ifndef EXTERN
#define EXTERN extern
#endif

EXTERN int somedata;
/* ... */

/* In ONE source file */
#define EXTERN
#include "myHeader.h"

That way you don''t need to duplicate your data declarations.

Share this post


Link to post
Share on other sites
I use extern''s for a few things in my programs. I think it takes too much cpu power to count the number of polygons being displayed by adding it to my octree class and then having it recursively add up, so I just use an external variable polycount from my main file to add it up (I''m sure it saves a minute amount of time). I also use them in making things I declared using #define but want them used in multiple functions (like that really long #define for multitexturing that I can''t remember off the top of my head). They''re only evil if you like Java as a primary programming language, and in that case you''re weird

Share this post


Link to post
Share on other sites
I use extern for certain game elements that pretty much everybody needs access too (like the screen, managers, the current game space...). There are ways to get around that, but sometimes not.

Share this post


Link to post
Share on other sites
I use extern only for my global variables, as virtually any other use of it would violate encapsulation (my programs are largely object-oriented). Of course, you''re using C...in which case lots of global variables might be your deal if you''re writing games. They are fast and always accounted for, never leaving you guessing at how much overhead you may be acquiring.

Anyway, I keep each of my object classes in their own files...you could use the same school of thought in c by keeping different kinds of functions in various files...functions for error logging, functions for main character powers, bad guy a.i., what have you.

Hope this helps.

Share this post


Link to post
Share on other sites
That''s like saying "is goto evil?" Nothing is evil, only (in)appropriate.

Share this post


Link to post
Share on other sites
Thanks for the input.

twanvl: I''ll stick to it.

quote:

That''s like saying "is goto evil?" Nothing is evil, only (in)appropriate.



Well, of course ''extern'' is not explicitly evil but I do see to often ppl who use or better said misuse it to speed up the working process and violate a well structured design just to end up in a pile of nothing, that is undebuggable.

Especially when more than one programmer is working on a project this turns into a threat in no time.

"Goto" is a good example. And by the way ''extern'' might be (in)appropriate, but "Goto" IS EVIL .


Thanks again.

Share this post


Link to post
Share on other sites
extern ... goto ...
It''s all part of the language guys. They both have their place. The only question is "Can it be done better without the use of these keywords?" Often the answer may be "yes". But both of these keywords definately have their place, as rare as they may be.
Just like ''volatile'' isn''t used much, but when you need it you need it.
And ''continue'' that''s not used a whole lot, but there are times when it REALLY is the best way to go.

Lets not make this another one of those long boring language bashing threads. Practically all parts of all languages have their place, but anyone can use something for the wrong purpose.

Share this post


Link to post
Share on other sites
btw, no-one is really going to be able to best answer your question, because it all depends on what is in the files you speak of.
From the sounds of it I''d say leave it as is!

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!