I have heard in numerous places that global variables should always try to be avoided; first question is Why? |
1) Their usage makes your program restrictive. IE: if you write a function to use globals, then your function will only work with those globals.
Say for example you're writing a game and you want a function to deal the player some damage:
1 2 3 4 5 6
|
int player_hp;
void DamagePlayer( int damage )
{
player_hp -= damage;
}
|
Seems harmless, right?
The problem now is that 'DamagePlayer' only works with 'player_hp'. It can't damage anything else. What if there are multiple players? What if you want to damage an enemy? Would you have to write a seperate function for each player/enemy? Talk about a waste.
It's much better to
minimize the scope of all variables so that they're generic enough to apply to anything within reason. A much better approach to the above would be to pass 'hp' as a parameter by reference:
1 2 3 4
|
void DealDamage( int& hp, int damage )
{
hp -= damage;
}
|
Now you can deal damage to any player/enemy/whatever with the same function.
Another, more common example is with I/O:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// not so good
void LogReport()
{
// a function to give a report to the user
// let's say the report is in a string called 'Log':
cout << Log;
}
/*
again -- the above seems harmless -- except it logs to cout (global)
What happens when you want to change your program to log to a file instead?
You'd have to go back and change the above
*/
// better
void LogReport(std::ostream& logout)
{
logout << Log;
}
// now you can log to any ostream (a file, cout, cerr, whatever)
|
------------------------------------------
2) They make the entire program assume responsibility for a variable's state. When everything in the program has access to a variable, then your code must assume that anything in the program can change that variable.
This is harder to come up with an example for... but trust me it's an organizational nightmare for moderate/large programs.
When you keep the scope as small as possible, it minimizes the responsibility that any single function has. It keeps code straightforward and simple.
When everything is global, functions usually end up taking on too much responsibility and do things they shouldn't. You wind up with spaghetti code where everything is confusing and all tangled up.
Second question, the stream insertion operator does not read blank spaces which is why you recommend cin.getline() or are there less obvious reasons as well? |
To be honest, I know jack squat about istreams. I never use them.
I recommended getline here because he looked like he wanted to read the entire file, so it made more sense to read it as large of chunks as possible. IE: line at a time made more sense than word at a time.
I also remember hearing things about >> leaving stray characters in the buffer under some circumstances (but I think that's just if you try reading an int or something) -- but like I say I don't really know because I never do it.