When writing code and one has to declare a variable or class, is it good programming practice to declare it outside the main function or inside the main function? So far I have been declaring class names and its associate object outside the main function as well as variables. If I am in a specific situation where the scope of the variable matters, I will declare it elsewhere, but for general knowledge and knowing what to do and what not to do, I am unsure about it.
Normally you should declare variables in as local a scope as possible, which means that you should usually declare the variable a function close to first use. It is considered bad practice to use global variables, except in rare circumstances.
You will usually define a function prototype or a class outside any function so that the function or class can be used anywhere in the program.
int add(int a, int b);
sum = add(1, 2);
int add(int a, int b)
int sum = a + b;
Sometimes it's just more convenient to use global variables. Like if for example you have a bunch of variables that persist throughout the program and have to be passed to all sorts of functions then I would rather use global variables (wrap them in a namespace of you want). It makes the function calls much more readable and at the same time it isn't at the cost of anything.
It makes the function calls much more readable and at the same time it isn't at the cost of anything.
Why do you think using global variables makes function calls more readable? IMO, it is the opposite, except in very trivial programs.
By the way there is a definite cost of using global variables, trouble shooting a program littered with multiple global variables is much more difficult, and it is easier for a bug to find it's way into your program because the value of those global variables can change anywhere.
If you have a lot of constants that are determined run-time and that need to be passed around to different functions then I have the preference of keeping them global. It wouldn't be hard to debug as they are constants. And it would make the program more neat to have them global because you wouldn't have to pass around those constants from main function to other functions.
But this my opinion. I just wanted to say that you should not live and die by the rule that you shouldn't have global variables. There can be situations where they are more appropriate and there will be situations where they're just more convenient.
If you have a lot of constants that are determined run-time and that need to be passed around to different functions then I have the preference of keeping them global. It wouldn't be hard to debug as they are constants.
First you didn't mention the constant aspect in your last post, having compile time constants global is not a big problem.
But what do you mean by "determined at run-time"?
There can be situations where they are more appropriate and there will be situations where they're just more convenient.
Yes there are reasons to use global variables but those reasons are few and far between, "convenience" is not, IMO, a valid reason in and of itself.
My rule is that constants can go anywhere (should be in a namespace, if the program is large) including the global scope, but never a variable.
there is only 1 really good use case for a global variable. They can provide a performance boost in low level code under a few very specific scenarios. Just about everything else is an indication of a design problem. And if you do find that using one has some sort of merits, wrap it up tight; put it in a class by itself as a static member, for example.