No. I admit it; you were right all along.
........ parameters!
........ local statics!
Globals for the WIN!
Soma
Printable View
No. I admit it; you were right all along.
........ parameters!
........ local statics!
Globals for the WIN!
Soma
Hurray. Then gimme somes Like's already.
After this beating, I need a booster.
If I had a nickel every time I've heard that one, wow, let me tell you, I'd have 5-cents. ;)
(sorry I called you a jerk)
I have read much, but not all the items in this thread. So if this duplicates something already said, I am sorry. I also realize that many are trying to draw this thread to a close. I saw that someone suggested using a variable name tag to make it clear which variables are globals. This is also a good idea.
I am not fluent in C++ but I am in C. So if you are using C++ in:
a) a fully object oriented way with full use of classes, templates, containers and the standard library or
b) using it mostly as C with Classes,
then my advice may or may not be applicable.
If you are going to use globals (yes, there has already been enough discussion about that part of the topic) and each of the global variables is going to be used in almost all of the same places, then using global.h (or something similar) is fine.
However, if only a limited few are going to be used in a small number of places, I would suggest that you NOT put all the declarations in a single file. I would suggest that you split them into several files. I would also consider making that header file with the global variable declarations separate from the header file with the function and class declarations. Here you may want to use <name>_g.h or g_<name>.h for the file name. The likely <name> is the name of the .cpp file where you are putting the initialization. Then include the these individual files only where they are needed to read or modify the global variables. Then it minimizes the places that specific globals can be accessed (and accidentally be changed) and this will minimize the coupling mentioned earlier in the thread.
do you really expect people here to coddle you? what have you done in your limited tenure on this forum to merit such treatment? if you want to feel good about yourself, go volunteer at a soup kitchen. if you want intelligent discourse on the merits (or lack thereof) of using global variables, including comments contrary to your own opinions, then this is the place for it. if you require emotional validation for your technical expertise, you're going to live a cold, lonely life.
pheininger,
Thanks for the plug, but trust me, the Professionals seem to only listen to those with a high number of posts.
Elkvis,
"if you want intelligent discourse on the merits (or lack thereof) of using global variables"...
YES, let's start that up again today.
I will WIN this time.
I have the right weapon now.
It came to me last night before going to sleep.
My subconscious is, hmmm... "freakin awesome" - HA.
Are you all ready to lose this debate now?
One word...
namespace
Feel free to explain why namespace exits if a class can "encapsulate" everything.
You will fail. Period.
Accept defeat, HA!!
Looks global to me:
Namespaces - C++ Documentation
The forum's own website:
FAQ > C++ Namespaces - Cprogramming.com
WOW, I beat you guys.
I can't believe it.
Not exactly. There are no global variables in that article. Namespaces are primary applied to types, not instances. Yes, you could declare a variable in a namespace, making it effectively a global, but that's the same bad practice.
Put another way: yes, your classes are defined globally. Of course. Just like types such as "int" are defined globally. That's not the same thing as a particular int instance. Eg, C++ strings are defined in the standard namespace (hence, accessible globally as std::string, a type). But AFAIK, there are no global variables defined in the standard namespace.
Basically you are conflating objects with classes, but I am sure you know the difference. Going back to something from your post #22:
Defining struct myStructData globally* is fine and dandy. Probably necessary.** It's declaring vector StructData globally that is the issue.Code:struct myStructData{ string name; int num; bool isReady; };
extern vector<myStructData> StructData;
* or in a namespace, which is better. The type is still globally accessible, like std::string, but organized into a collection of related things. This way, if you declare a type such as myStructData in namespace myNamespace, you can declare a totally different myStructData in some other namespaces and the names will not collide. That's the point of namespaces. Otherwise, in a large project using a lot of third party libraries, everyone would have to be paranoid about the names they used for fear of redefining something with the same name somewhere else.
** you can nest a class definition inside another class definition, making it an inner private class which is not available globally. This is a good practice if the class has no application beyond the class it is nested inside. You can do the same thing with structs, typedefs, etc.
Make sense?
Feel free to explain what is encapsulation and what are namespaces, and the reason will become obvious :)Quote:
Originally Posted by motocross1
There are two "global variables" in that article, but they were declared in an unnamed namespace. Consequently, they suffer from the same problems as global variables of the "declared extern" variety, but with the mitigating factor that the reader only has to be concerned about the functions in the translation unit changing them.Quote:
Originally Posted by MK27
I would rather say that namespaces are just a way to group names so as to avoid name collision.Quote:
Originally Posted by MK27
std::cout and std::cin are effectively global variables in the std namespace.Quote:
Originally Posted by MK27
Motocross1:
Aside from the very critical and important points that some of the other guys have made about encapsulation and coupling, there is another downside to using globals. When declaring a global variable you're adding that variables name to the global namespace, which essentially means you can't use that name for anything else if you want your code to be readable or understandable at all.
Consider the following code:
One would have to step through the code line by line, or actually run it, to be certain of what the output would be. Now imagine that the variable isn't called "myNumber", imagine that it's called "index" or "key" or "pi" or some other commonly used variable name. You're essentially removing that name from the pool of available variable names for all the code that is written after or before you declared that global variable, unless you don't mind the source looking like my above example. This means you will have to pick less fitting names or resort to some obscure pseudo-hungarian naming scheme (such as "g_index"), which in turn means your code will become less readable.Code:#include <iostream>
int myNumber = 8;
int doMath(int myNumber = 5)
{
myNumber += myNumber;
return myNumber;
}
int main()
{
int myNumber = 3;
myNumber = doMath(myNumber);
myNumber += doMath();
std::cout << myNumber;
return 0;
}
This isn't the only problem. Imagine if you were ever to write some code that was to be reused by someone other than you, if you put global variables all over it, it is more than likely that there will be some sort of naming conflict when someone else has named their function the same as one of your global variables. Now i know you've said that the code you are writing is only for yourself, but you might aswell start learning proper techniques now. If you stick with global variables now, you will have trouble down the road when someone demands that your code lives up to some form of expectations. (Such as being even slightly readable)
Now all this i wrote is only partly why global variables are frowned upon, as others have stated that there are other more critical problems with global variables.
Also on a sidenote, you seem to think that "the professionals" on this board are trying to force you to structure your code according to some strange illogical agenda. Just know, that global variables are frowned upon not only on this board, but in the entire professional computer-science industry, globally.
Even the Wikipedia article on the matter speaks poorly on global variables. Take a step back and think about this, the entire world disagrees with you, maybe you should try and re-read this thread with an open mind, and actually make an effort to understand what some of the others have touched upon.
Or....you could continue to argue that you are right and the world is wrong.
No tish. There is a ton of great information and explication in this thread. Motocross1, you are wasting a big opportunity to understand some fundamental concepts by not asking specific questions about the parts that are unclear! There is nothing wrong with saying, "I don't get this", or "could you explain this further", etc, rather than skipping over them and presuming (or pretending) that they must be irrelevant.