[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Global variables - are they always evil?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 10
Thread images: 1

File: George-Soros.jpg (218KB, 3216x2412px) Image search: [Google]
George-Soros.jpg
218KB, 3216x2412px
I found a game I wrote in LISP a few years back where I used no global variables. Instead I put everything in a hash table "world" that I passed as an argument to various update functions that returned a new hash table with the new values. There was only one assignment statement of the form (setf world (update world)) in the main game loop.

The game ran like crap. Should I have used global variables instead?
>>
>>59942428
>(setf world (update world))

Why would you use SETF if your (update world) function returns a new world value?

Obviously if you go with the "copy everything" mentality you incur a speed and memory penalty for it. But your (should) have fewer bugs.
Global variables are not always evil, the point is to segregate functions that modify them from functions that do not modify anything outside of themselves.

Also, your game could have had other issues besides a giant hash that would cause it to run slow.
>>
>>59942428
There's some pretty good discussion about this in K&R, isn't there one in your religion's scripture?
>>
>>59942428
global vars are ALWAYS evil because they introduce global state. Managing state is a hard problem, and if it's even global - u r in some deep, deep shit.
Usually, if you ever need to access some state globally - you want a DB. If you want it in memory - add caching.

Simple as that.
>>
>>59942547
But since in a game most functions modify the game state, wouldn't it be simplest to make everything a global variable and have all functions directly modify the global variables and return nothing?
>>
>>59942428
Yes global variables are always bad outside of C (not C++). Yes, your "solution" was bad. I would look into component based design and event based messaging. Branch out from there
>>
>>59943330
global vars are ALWAYS bad, no exclusions. You're right in the remaining part of your post, though
>>
>>59942428
Basically they are only useful as locks in parallel computing.
>>
>>59943685
which is also a bad practice. Sane programmers use CSP or actor model
>>
>>59942428
Global variables are good for your code, it makes it easier to share them across the whole program scope.

I am a certified Javascript Senior Architect.
Thread posts: 10
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.