Saturday, 31 August 2013

When are global variables actually considered good/recommended practice?

When are global variables actually considered good/recommended practice?

I've been reading a lot about why global variables are bad and why they
should not be used. And yet most of the commonly used programming
languages support globals in some way.
So my question is what is the reason global variables are still needed, do
they offer some unique and irreplaceable advantage that cannot be
implemented alternatively? Are there any benefits to global addressing
compared to user specified custom indirection to retrieve an object out of
its local scope?
As far as I understand, in modern programming languages, global addressing
comes with the same performance penalty as calculating every offset from a
memory address, whether it is an offset from the beginning of the "global"
user memory or an offset from a this or any other pointer. So in terms of
performance, the user can fake globals in the narrow cases they are needed
using common pointer indirection without losing performance to real global
variables. So what else? Are global variables really needed?

No comments:

Post a Comment