Is it better to declare global variable or declare variable multiple times?

So I got some code, it looks a little like this...
1
2
3
4
5
6
7
8
9
10
11
void Func(int i)
{
	//calculations with i.
}

int main()
{
	int i;
	Func(i);
	//more calculations with i.
}


But is it better if I use globals instead?

1
2
3
4
5
6
7
8
9
10
11
12
int i;

void Func()
{
	//calculations with i.
}

int main()
{
	Func();
	//more calculations with i.
}


Just wondering if one of them is a worse programming practice for any reason.
Core Guidelines:
Avoid non-const global variables
...
Note: The rule against global variables applies to namespace scope variables as well.
...
Note: The rule is “avoid”, not “don’t use.” Of course there will be (rare) exceptions, such as cin, cout, and cerr.

See: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-global
But is it better if I use globals instead?
Certainly not. With global variables you get the most surprising results. So Keep it as local as possible.

https://www.tutorialspoint.com/why-are-global-variables-bad-in-c-cplusplus
https://www.tutorialspoint.com/Why-should-we-avoid-using-global-variables-in-C-Cplusplus
Setting aside that those two snippets have different semantics, yes, your default policy regarding globals should be "don't use them".

Things that globals are good for:
* Storing constant data that the program uses in various functions. For example: https://github.com/Helios-vmg/cpp-crypto-algorithms/blob/master/crypto-algorithms/aes.cpp#L6
* Storing configuration that the program will use in many disparate places, such that it would be impractical to pass it to every bit of code that needs it.
* Storing statistics that are not the main purpose of the code (e.g. how many times a certain function is called) and thus should not be part of the function signatures.
* Storing objects that must only exist exactly once. For example, if you have a class to represent the computer's file system hierarchy, such a class should only be instantiated once.

You should never use globals to store intermediate calculation results. That will only lead to trouble down the line. For example, what happens if the Func() that uses globals is being executed simultaneously from two different places?
Objects offer a way to control globals and limit access if you must use one for some reason. Ironically, while doing the same thing, this is not frowned upon as much (static accessors to a static class variable)
As a language note, I argue STRONGLY against reusing variable names apart from basic loop counters (i,j,k usually) and similar.

your first snip could be written using different names.
1
2
3
4
5
6
7
8
9
10
11
void Func(int &iref)
{
	//calculations with iref.
}

int main()
{
	int i;
	Func(i);
	//more calculations with i.
}


which by the way is what you were trying to say I think, but you left the & off the parameter. The way you wrote it, mains "more calculations with i" revert back to its original value before func was called... you may as well not call func as all it did was discarded.
Last edited on
Topic archived. No new replies allowed.