I know what they are, but I am not sure which I should use.
✓ ANSWER ACCEPTED
How do I know when to use a local or global variable?
1 answer
✓ Accepted answer
My answer to this is always use a local variable (except in the cases where a local variable won't work, which aren't that common) except for appropriate constants.
They can be somewhat faster and they also help avoid problems with functions poisoning each other's variables accidentally because of (global) variable name reuse.
Usually, good code is split up into many completely independent functions or modules. If you are allowing yourself to use a lot of global state, then you are making functions not only dependant on each other, but on how they are implemented - which can lead to sloppy and confusing design.
This discussion is preserved as it was. New answers, comments, and votes are closed.
