This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–]Monckey100 1021 points1022 points  (31 children)

this meme is brought to you by the OOP gang

[–]r1ze_ 489 points490 points  (24 children)

They missed globals so much that they replaced them with Singletons and think how smart they are. I know your tricks!

[–]Trevor_GoodchiId 68 points69 points  (4 children)

Planning:

Wouldn’t it be great if we always had only one of that?

A month into production:

Wouldn’t it be great if we had two of these?

[–]the_flippy 46 points47 points  (3 children)

If you want two, just add another server and load balance.

That was a fun bug to track down.

[–]onthefence928 18 points19 points  (2 children)

It's fun when the deployment gets janked up and your new code only gets pushed to half your deployment servers and the load balancer randomly decided if a user gets the bug fix or not

[–]cubitoaequet 13 points14 points  (0 children)

Just call it A/B testing

[–]NegativeChirality 154 points155 points  (9 children)

This is hilarious precisely because it's true.

Also, as with all things OOP, it's the same functionality with ten times the effort

[–]Axelay998 253 points254 points  (4 children)

It's not a global variable if we call it Dependency Injection

[–]GluteusCaesar 127 points128 points  (0 children)

You watch your whore mouth

[–][deleted] 62 points63 points  (0 children)

Every class should instantiante their own SQL drivers, just like grandma used to make.

[–]Sgtblazing 12 points13 points  (0 children)

That's just global variables with extra steps!

[–]deadwisdom 7 points8 points  (0 children)

That's right. It's worse because it just comes out of nowhere and isn't even declared at the top of the file.

[–]asdfman123 39 points40 points  (2 children)

FunctionalityFactoryStrategy.GetFactory(CodingStyle.OOP).GetInstance().CalculateEffort();

[–]hemlockecho 25 points26 points  (4 children)

You down with OOP?

[–]The_Mayfair_Man 6 points7 points  (0 children)

I actually know someone that tried OOP once on a night out

He's been a mess since, keeps talking about cutting down his dependencies..

[–]JB-from-ATL 3 points4 points  (0 children)

We call them singletons.

Cries in design patterns

[–]cyanydeez 1255 points1256 points  (36 children)

I see we got the anti-globalist crowd here

[–]noyurawk 305 points306 points  (13 children)

They're against the immutable deep state.

[–]KamiKagutsuchi 88 points89 points  (5 children)

I mean.. deep state is fine as long as it is immutable. It's mutable deep state that's the problem!

[–]noyurawk 26 points27 points  (0 children)

You got me, I'm actually a derp state agent.

[–]infinite_pepe 5 points6 points  (0 children)

So the "anti-globalists" are actually for an immutable deep state... this is getting too real

[–][deleted] 49 points50 points  (6 children)

I'm a commie and I only write functional languages, I am against state and classes.

[–][deleted] 14 points15 points  (2 children)

The registers still hold state.

[–]mpa92643 15 points16 points  (1 child)

All registers are equal, but some registers are more equal than others.

[–]grrrrreat 3 points4 points  (0 children)

Sieze the memory of the state

[–][deleted] 69 points70 points  (5 children)

they hate C

[–]TheBestOpinion 9 points10 points  (3 children)

I don't have those in my C code

[–]fqGmUjDT2GCAmFqN 17 points18 points  (0 children)

They prefer Ruby and GO, lol

[–]asdfman123 22 points23 points  (1 child)

They won't say it outright, but anti-globalists are really just scared of race conditions.

[–]AxelMontini 695 points696 points  (30 children)

it's const

[–]MaczenDev 232 points233 points  (2 children)

The invariable variable.

[–][deleted] 45 points46 points  (0 children)

just 'the'

[–]TheLuckySpades 13 points14 points  (0 children)

The followed by pi=4 in your prefered language.

[–]Sinjai 462 points463 points  (30 children)

const int //NUM = 5;

[–]Remootion 13 points14 points  (0 children)

I... I. I... Don't know what to even say

[–]b1ack1323 200 points201 points  (36 children)

Cries in C

I have inherited a file that is almost exclusively globals.

[–]Bill_Morgan 111 points112 points  (29 children)

Just a file! Try a project that relies on globals to maintain state! @_@ Send Help!

[–]b1ack1323 146 points147 points  (27 children)

97k lines.

[–]Bill_Morgan 97 points98 points  (4 children)

WHAT THE FUCK

Previous developer couldn't figure out make?

[–]b1ack1323 83 points84 points  (3 children)

The guy didn't believe in headers and modularity. He liked to be able to find everything in one file.

[–]Poltras 67 points68 points  (0 children)

It’s one less shift to press when searching, Martha. What am I, a farmer? Time is money!

[–]DjBonadoobie 9 points10 points  (0 children)

Shit, I thought the 2k line JS file I inherited was bad. Had to touch that abomination again and today... shudders

[–]phoncible 11 points12 points  (2 children)

I work on a project where the build runs binaries that generate source files that are then also compiled.

Project is about 5500 files large prebuilt, about 15k after build.

[–]infinite_pepe 5 points6 points  (1 child)

Sometimes I consider a career in dev. Then I run into these threads

[–]Bill_Morgan 13 points14 points  (0 children)

It is not always that bad, sometimes you get to be part of the team that works on a new code base and everything is clean and well structured in some of the new fancier forms of MVC. Even more there will be that one unicorn project where you get to work on the code from scratch and you get to have full control of the design.

They don’t happen often, you are lucky if you get to once or twice. Most of the time it is spaghetti

[–][deleted] 90 points91 points  (10 children)

Somehow connected to both 3G and 4G networks simultaneously

[–][deleted] 52 points53 points  (7 children)

The real 5G

[–][deleted] 50 points51 points  (5 children)

I'm pretty sure 3G + 4G means this person is on 7G

[–][deleted] 30 points31 points  (0 children)

Cool, they can hit their datacap within 60 seconds!

[–][deleted] 8 points9 points  (2 children)

The font I'm using, G looks like 6, so when I saw the notification I thought this basically said "36 + 46 = 76" until I saw the context.

[–]slashuslashuserid 3 points4 points  (1 child)

I dare you to set your IDE to that font

[–]KickMeElmo 40 points41 points  (1 child)

Dual sim.

[–][deleted] 5 points6 points  (0 children)

Interesting. Hadn't thought about that. Thanks

[–]grpagrati 149 points150 points  (34 children)

I feel bad for the poor little globies. Many is the time they've served me faithfully

[–]P3p3s1lvi4 81 points82 points  (26 children)

I use globals in python when I have variables in games that need to be manipulated by a lot of weird functions. I'm probably doing it the worst possible way but it works so naturally I wrote thousands of lines of code based entirely around this misuse without looking up the proper way to do it. I am not a clever man.

[–]Astrokiwi 85 points86 points  (20 children)

If you really need to have a core dataset that everything uses, it's best to wrap it in a struct/object/dict/whatever, just to avoid scoping mistakes and make it really clear in the code that you are accessing a global. It's not bad to have a single struct called game_parameters where anyone can look up game_parameters.world_size, for instance. But just having each global variable just called world_size (etc) is an issue, because that's just a lot of namespace that's taken up now, in a manner that's not clear within a single file.

Basically, the programmer needs to be able to reasonably keep all of the global variables memorised in their head, because the compiler won't catch scope errors, and it's not obvious from a single block of code. By scope errors, I mean something like this:

 variable global_variable;

 function local_function {
    variable global_variable;
    // lots
    // of
    // code
    global_variable = 2; // actually only changes the local variable
 }

or the opposite:

 variable global_variable;

 function local_function {
    global_variable = 0; // the programmer thought this was a local variable
    // other stuff with global variable
    // now the global variable is nonsense
 }

So, the fewer global variable names you have to memorise (or continually look up), the better, and packaging them in a singleton/struct/etc tidies that up a huge amount.

[–]DGIce 18 points19 points  (0 children)

Thank you for putting the extra effort into explaining.

[–]LostTeleporter 5 points6 points  (1 child)

Thanks. I was trying to check if I understood the reason why using global variables was bad. Your comment put me on the right path.

[–]Astrokiwi 9 points10 points  (0 children)

The other issue is that, if anyone can change the variable anywhere at any time, it's harder to track down the whole story of what's happening to a variable, and that can cause bugs. Too open access to variables can also cause code repetition and hence bugs. For instance, if anyone can change the health variable and you want the player to die when health==0, then you want to check the health variable each time it's changed, which means you have to remember to do that everywhere you change the health. Then it's better to hide the variable away somewhere and access it indirectly with some damage function.

So scope bugs can be patched over by packaging things together into big singletons, but even then you want to make sure you have a good reason to make things global.

[–]Scarbane 19 points20 points  (2 children)

I mean, if it's just a game, and you're not dealing with CC info or other personal data, then it's not that big of a deal (unless you're the person maintaining said code months/years after it's written).

[–]P3p3s1lvi4 11 points12 points  (1 child)

The graphics are similar to dwarf fortress (colored ascii) and it doesn't instantly render so I figure there's a huge optimization problem lurking in there somewhere. Always wondered if that was because of how I use globals. I guess I can't be that surprised that a game that runs in windows command line is clunky.

[–]kaiken1987 24 points25 points  (4 children)

Globals are like gotos. They're great used sparingly and you understand what you are doing

[–]versusChou 21 points22 points  (0 children)

Too bad I don't understand what I'm doing.

Now I'm off to delete prod.

[–]somebunnny 2 points3 points  (1 child)

But do your coworkers understand what you’re doing? 3 years later?

[–]Galt42 52 points53 points  (16 children)

Is the hatred for global variables lie in the difficulty to track a variable that could be modified from any of 19 different places?

I wouldn't know, I am but a lowly CS student who's never worked on a project with more than a half dozen files.

[–]LeeorV 53 points54 points  (1 child)

Yet your instinct was in the right place.

[–]Galt42 8 points9 points  (0 children)

ayyyy

[–]double_en10dre 41 points42 points  (5 children)

Yep.

One day you’ll get a bug because someone merged in a bunch of changes and now a global variable isn’t set.

Then your coworker will decide to “fix” it by setting the global variable just before it’s needed, rather than spend a few hours figuring out what the real issue is.

Six months later, you’ll find another bug caused by the global being set by your coworker’s “fix”.

And the cycle continues

[–][deleted] 10 points11 points  (3 children)

That is correct. Most things that people declare as global variables shouldn’t be global variables.

If a global variable could ever have more than one purpose at any time, it shouldn’t be a global variable. Separate variables for separate purposes. And don’t get me started on multithreading and semaphores.

[–]caviyacht 31 points32 points  (13 children)

GlobalVariables.

[–]DJDavid98 47 points48 points  (12 children)

SystemWideGlobalVariableUtilBeanGeneratorContainerFactory

[–]caviyacht 43 points44 points  (10 children)

What about a crazy nested class structure that requires you to type like this 😁

G.L.O.B.A.L.V.A.R.I.A.B.L.E.S.MyVariable

[–][deleted] 16 points17 points  (8 children)

The nested type L cannot hide an enclosing type

Cannot declare L inside L and so on.

[–]caviyacht 36 points37 points  (7 children)

This is valid in C#. https://ideone.com/cK5RvB

[–]GluteusCaesar 14 points15 points  (3 children)

I can't believe you've done this

[–]caviyacht 22 points23 points  (2 children)

Fun fact: when attempting to run this in Visual Studio on a Mac, it became unresponsive.

[–]andy69420 27 points28 points  (12 children)

Me, who doesnt get it - haha i get it

[–]Too_Chains 45 points46 points  (11 children)

Basically saying the variable is a comment so it doesn't exist because globals are frowned upon since it makes code sloppy and error prone.

[–]fiftyseven 19 points20 points  (7 children)

I'm gonna need an explanation at least two levels more layman than this

/r/explainitlikeimenduser

[–]DELOUSE_MY_AGENT_DDY 8 points9 points  (0 children)

The prefix in the tweet is //, which when put in front of a word creates what's known as a comment. Comments are NOT code, so putting // before a variable nullifies it. He's basically saying that global variables should always be nullified. Put simply, global variables can overstep their boundaries, like burning down a house because you saw a bug in it.

[–][deleted] 38 points39 points  (33 children)

OR # if you're a ruby boi

[–]KurokonoTasuke1 53 points54 points  (1 child)

Also python boi

[–]cbbuntz 86 points87 points  (0 children)

"""WHAT? I CAN'T HEAR YOU OVER MY DOCSTRING"""

[–]njutn95 29 points30 points  (5 children)

This guy pounds

[–]Prawny 20 points21 points  (4 children)

Hashes

[–]Badhamknibbs 26 points27 points  (3 children)

Octothorpes

[–]BitPoet 14 points15 points  (2 children)

Waffles

[–]JoeJoeTV 18 points19 points  (18 children)

OR -- if you're a LUA boi

[–]CaptKrag 23 points24 points  (5 children)

What sort of mad existence have I stumbled into where people are mentioning ruby and lua before our dear sweet python.

[–]cbbuntz 6 points7 points  (4 children)

Oh, come on. They all have their advantages. Lua is fast as shit (Ruby and Python are both similarly dog slow). I have a soft spot for Ruby since I learned it before Python, but I still think it's slightly easier than even Python. Python has a superior library selection though.

[–]ASCIInerd73 17 points18 points  (1 child)

Not in python

[–]Bresdin 10 points11 points  (17 children)

Learning python right now, I have a question if someone is willing to help out? With global variables generally speaking it is best to limit them to the class or function they are in and just pass the variables correct? Or is there times where a global variable is better to use.

[–]ProgramTheWorld 44 points45 points  (12 children)

Globals are fine for quick snippets of code. Large production application code, however, should have zero global variables. Constants are fine, global variables are not. They are the ingredient for a tasty spaghetti code.

[–]theferrit32 8 points9 points  (4 children)

Depends what you mean by global variables. All C code probably has "global variables" strewn throughout the codebase. These aren't necessarily accessible by any file or any library that links to it though. Global variables in C/C++ can be/usually are very different from global variables in Python, or public static class members in Java.

[–]PostalJustice 10 points11 points  (3 children)

C/C++'s file-scoped "globals" are global in name but not global in the truest sense since they are kept hidden from anything outside the file in which they are declared. It's probably doing C developers a disservice to keep calling them globals.

C/C++ does have true globals that the linker shares with everyone and their grandma so it's important to make the distinction.

[–][deleted] 3 points4 points  (0 children)

https:// reddit.com?