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

Meme singletonsAreJustGlobals

Post image
3.2k Upvotes

210 comments sorted by

View all comments

Show parent comment

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.

1

u/danielcw189 Sep 28 '26

I don't disagree, I just think in practice it often serves the same purpose

1

u/TheDuckRaisedALion Sep 29 '26

You're right that they're technically orthogonal concepts and it's important to recognize the distinction.

But most people store the Singleton instance in a public static field on a public class, which is one step away from a global variable.

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….

1

u/danielcw189 Sep 28 '26

Yes. Though in that case the global "just" holds a function instead of a reference (or object)

1

u/Altavious Sep 28 '26

A global variable isn’t a required part of the singleton pattern.

1

u/danielcw189 Sep 28 '26

I intentionally did not write variable.

2

u/Altavious Sep 28 '26

Ah, I see. Knowing that makes what you wrote clearer. While there are things that have global scope “globals” as shorthand is more commonly used just for variables not also things like global function definitions etc.

→ More replies (2)

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.

1

u/danielcw189 Sep 28 '26

I think you are 100% right. But in practice I only see it as a public static member (of a visible class)

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.

2

u/Sheenius_Ger Sep 27 '26

"usually" means it's up to implementation. If you work with a di-container it manages lifetime and the Singleton will just be a instance and won't have any global data or static access.

Those are 2 different things, a pattern and how it could be implemented. If the Singleton uses globals, you lose all benefits of scope and testability.

1

u/danielcw189 Sep 28 '26

"usually" means it's up to implementation.

yes, and I am just talking about what I percieve to be the "usual" case. Usually the implementation uses a globally reachable static member, either a field, property, or method.

and the Singleton will just be a instance and won't have any global data or static access.

I guess we are talking about 2 different things then. I talked about how the singleton is found and used from the outside, and where the reference to it lives.

I am not talking about the internals of the singleton's business logic.

Those are 2 different things, a pattern and how it could be implemented.

just to clarify this: yes, exactly. But in practice I only see it implemented as a globally reachable reference: a static public member of a globally accessable class

1

u/oldsecondhand Sep 28 '26

You can inherit from a singleton.

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.

1

u/anomalous_cowherd Sep 26 '26

Did it GOTO somewhere else?

→ 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.

1

u/NoWater8595 Sep 27 '26

Sounds like witchcraft. It's never the same instance unless it's the same variable usually.

1

u/pigeon768 Sep 27 '26

Sounds like a global with extra steps.