Global Question

Miscellaneous Forums/General Discussion/Global Question

Not a problem, but just curious what the problem with
Global is. I know there's some problem with Global, I'm
just not sure what it is.

I think you mean goto

There is nothing "wrong" with global; sometimes it is useful an sometimes it is not.

However, it is very often misused. Lots of developers will use Global variables for stuff like multiple return values from a function. This is very ugly and wrong!!
There isn't really a speed hit to care about (though I believe there is a Slight difference in speed), but the difference is readability, cleanliness and just not having dumb looking code.
If you have global variables doing too much, you have clashing variable names. This is where things like
updater2collisions_objectcollided_forces(updater2collisions_objectcollided(1))
come from.

Variables should be pushed around by return values and arguments whenever possible; it's the natural way.

This is also one of the reasons people like Methods in OOP, which is the idea of having a sort of function inside of a Type. Now functions are not thought of at the global level, but as parts of objects, so naming them (for one thing) is a lot more sensible!

Picture it like this:
You have a huge group of people, everyone is doing a very different task to achieve a single goal. There is a lot going on.
The problem is that everyone is separated by a huge distance even though they all need someone else to get their job done. It's an assembly line sort of thing.
Now, which is more efficient: Every single person screaming at the top of his voice to communicate with the one other person he relies upon (thus drowning out everyone else who is doing the same thing); or each person quietly walking over to the other important person he relies upon, and whispering in an indoor voice what needs to be said, then going back to his work in overall less time because he does not need to repeat himself to have his words heard.

Er... that was nonsense.
Anyway, be gentle with Globals :)


Variables should be pushed around by return values and arguments whenever possible; it's the natural way.

+1. Read up on 'functional programming', maybe learn something like lisp.

I still have a great deal of trouble avoiding the use of Globals. For small programs it's easy, but for very large projects I admittedly use more Globals than I probably should. However, I have yet to see any adverse effects to this. Perhaps there is a speed penalty, but if there is it is slight.

Avoid globals whenever possible.

When your source gets large, terror awaits you.

The only allowed globals are things that are nature standards like PI.

Even simple stuff like screenwidth and screenheight shouldn't be globals.. it may work for a single instance, but who's to say that you never will run two instances with different sizes of your product in a big window? Two instances, two sets of screendimensions.
While this might not apply to beginners, it's still good to learn to avoid globals, while still being a beginner. It'll pay off later.

I thoroughly agree with Mr. Pickledegoob :)

It's just natural!

Andy

Ok thanks