Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I know when to use a local or global variable?

Asked by
Validark 1580 Snack Break Moderation Voter
9 years ago

I know what they are, but I am not sure which I should use.

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

Ad

Answer this question