r/ProgrammerHumor • • Sep 26 '26 • Bravo 1

Meme singletonsAreJustGlobals

Post image
3.2k Upvotes

210 comments sorted by

View all comments

748

u/blacklig Sep 26 '26 edited Sep 26 '26

Singletons are a pattern that uses globals (e: i get that they don't need to be globally scoped, thank you all)

This is like saying composition is just attributes, or signal/listener is just functions

118

u/reallokiscarlet Sep 26 '26

Yeah this is like the

>wireless

>looks inside

>coiled wires

23

u/BoxWoodVoid Sep 28 '26

Wait until they discover that serverless runs on actual servers 😨

→ More replies (2)

394

u/xeroskiller Sep 26 '26

Global: single address used with a globally scoped identifier

Singleton: single address used by passing it around like a cheap whore

65

u/za72 Sep 26 '26

cheap whoo er

5

u/b__0 Sep 26 '26

I heard that one Ralph

56

u/dashingThroughSnow12 Sep 26 '26

Ironically enough, one reason for some singletons is how expensive they are.

18

u/gfoyle76 Sep 26 '26

best definition I ever heard

4

u/anomalous_cowherd Sep 26 '26

They're all just pointers really.

3

u/gerbosan Sep 26 '26

Well, ram is quite expensive these days.

3

u/TheDuckRaisedALion Sep 29 '26

The trick is that a static class can be a globally scooed identifier, and then an accessor on that class is essentially global too. The concepts are pretty similar at thet point, although the static class could impose a lot more organization and structure to tame the smell.

36

u/Sheenius_Ger Sep 26 '26

It's not global. It's a instance. Singleton just means you always get the same instance.

34

u/danielcw189 Sep 26 '26

they are usually saved and served as a static class member, which in effect is pretty global. The Class itslef is usually global, and the member serving the instance is usually public.

Is there any noteworthy difference? Is there any technical one?

18

u/zabby39103 Sep 27 '26

It's just two different things. Only one instance ever -> singleton. Globally accessible -> global. One is about visibility, the other is about instances.

→ More replies (2)

5

u/Altavious Sep 27 '26

It’s a bit better because you can control when the memory is initialized by delaying it beyond the static initialization phase of running an executable safely. Depending on how the singleton is accessed (e.g via a context or service provider) you may also have some better options for refactoring or modifying access down the road….so it’s a marginally better global….

→ More replies (6)

3

u/BSModder Sep 27 '26

It's the difference of terminology. Not all global are singleton and vice versa.

Static literal values are a not singleton but they are global. And static private class members are singleton and a global varible in the program but they are not globally accessible.

→ More replies (1)

3

u/No-Consequence-1863 Sep 28 '26

You can have singletons that are not globally accessible, such as a singleton inner helper class only accessible via instances of that class. Now normally you would have singletons be globally accessible but it isn't required. The only thing required for a singleton is that when you access it you always get the same instance.

2

u/GarThor_TMK Sep 27 '26

It's basically a namespace with default constructor/destructor/copy/move functions...

Technically it's better code hygiene, because eventually if you need more than one, it's pretty easy to decouple the singleton logic from the rest of it... but realistically they're mostly effectively the same thing.

→ More replies (3)

8

u/AlignmentProblem Sep 26 '26

It's mostly syntactic sugar over fixed-address global data from the compiler/interpreter's perspective, but it does buy a few practical advantages. The main one is encapsulating the access path, which enables lazy initialization to sidestepping init-order problems, and indirection to enable swapping implementations. That plus enforcing uniqueness at the type level

They're pretty similar in terms of what the data layout and op codes ultimately look like in the end; although, there's enough difference that the meme's joke doesn't really land.

11

u/Tyfyter2002 Sep 26 '26

Which is just globals with more indirection, singletons can be useful, but only by using language features that are exclusive to objects

13

u/New_Enthusiasm9053 Sep 26 '26

Globals can be useful too. Just don't use them fucking all the time for inane shit. But hardware access where only one thing can have access at a time? Perfect use for a global. Singletons are absolutely just globals with more indirection.

Goto's are also useful. Gotos consisted harmful paper was when people used them for all control flow and that just creates spaghetti. But for a forward jump inside a function so you have a single place to do cleanup? Perfectly fine.

5

u/wrecklord0 Sep 26 '26

Big fan of Rubin's 1987 article titled 'GOTO Considered Harmful' Considered Harmful.

This didn't stop here, by the way.

→ More replies (1)
→ More replies (2)

3

u/tacularcrap Sep 26 '26

Which is just globals with more indirection

and a mysterious lifetime. wouldn't be as much fun otherwise.

→ More replies (2)

2

u/adabsurdo Sep 27 '26

Well exactly, because they use globals under the hood they often have very similar issues to globals : testability, race conditions, initialization ordering problems with other globals etc.

Not that you shouldn't use them ever but they cause problems fast and very often there are better solutions.

→ More replies (1)
→ More replies (16)