What is usually done

Sage
Posts: 1,482
Joined: 2002.09
Post: #16
For what it's worth, I've come strongly to agree with OneSadCookie on this one. Global variables just end up making your life harder when you have to figure out why the heck global1234 has the value 2345. You have to look for every piece of code that assigns to it to figure out where it's getting set wrong. If you had instead passed a parameter to a function, you can trace where it came from.

Maybe I just make silly mistakes like this, but it only took a couple such bugs to realize how bad of an idea it is to rely heavily on globals. The code at my last job did this too, but when doing things like building giant SQL queries. :-| Days of debugging sometimes. Made worse by the fact that most of the "fixes" and "features" were added where ever would be the quickest place to add it. So bugs were often well spread out over the 300,000 lines of Java code without a lot of structure.

Use globals for constants that you write once and read many times, but not for tracking state where you read and write them often.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #17
(Aug 17, 2010 09:34 PM)AnotherJake Wrote:  
(Aug 17, 2010 08:46 PM)OneSadCookie Wrote:  I prefer to take the slight extra effort to eliminate all chance of evil.

Well I like to use globals because programming wouldn't be as fun without a reliable way to screw things up.

I believe people who maintains or work with your code don't think it is fun.

BTW, do you know software design patterns, for instance Singleton?
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #18
(Aug 24, 2010 03:03 AM)riruilo Wrote:  I believe people who maintains or work with your code don't think it is fun.
I don't use many globals and when I do it is clear what they are for. If you don't understand what delta time is for and that you shouldn't be writing to it, then that's your fault, not mine Wink

(Aug 24, 2010 03:03 AM)riruilo Wrote:  BTW, do you know software design patterns, for instance Singleton?
Of course, but I wouldn't recommend using a singleton to pass collision detection information around anymore than I would a global. Inappropriate usage of singletons is another great way to write poor code too, although at least you can control access to data.

It's not like I'm trying to advocate globals here:
I Wrote:While globals can be incredibly handy, they can cause problems...
I Wrote:You shouldn't need global variables for any collision detection.
I Wrote:If you need globals for it, then you're doing it wrong.
I Wrote:No globals needed.
I Wrote:Again, none of the approaches would require globals.

My standpoint has always been that they are available for use and you can use them when appropriate, which is to say, not very often. I would say the same thing for singletons.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #19
I didn't say Singleton was for that.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #20
I didn't say that you said that either Wink
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #21
Singletons are just as evil as globals. (Singletons *are* globals). The extra danger is, you may think you're being clever. You're not Rasp
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #22
Well at least we're being consistent here! Smile

I don't use singletons much, but when I do, it's usually for tool palettes and inspectors and shared UI stuff like that which will never ever need more than one instance.

[adding] actually, now that I think of it, there isn't any particular need for them to be singletons either since I do most of my communication with tool palettes and inspectors via notifications anyway. Doesn't really matter either way with these types of elements anyway though, if you ask me.
Quote this message in a reply
Post Reply