2

The Github C++ Core Guidelines say:

A global object is often better than a singleton.

I always thought the opposite. Something has changed since then in the C++? Or may be it's just another typo?

user207421
  • 305,947
  • 44
  • 307
  • 483
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
  • 2
    Random guidelines from the internet aren't worth any more than the explanation/justification they include. In this case it's none, so it's worthless. You're just as likely to find some other guideline that says the opposite. If you want reasoned arguments on a specific question such as "when to prefer singleton over global object or vice versa", best to ask it directly. – Tony Delroy Nov 09 '15 at 09:44
  • 3
    Look again. It says that *as an exception* to a rule *against* globals, entitled "Avoid non-const global variables". Don't take things out of context. – user207421 Nov 09 '15 at 09:45
  • 1
    @TonyD It's not random, because proposed by Stroustrup. – αλεχολυτ Nov 09 '15 at 09:47
  • 1
    @TonyD, but... but... the authors... – SingerOfTheFall Nov 09 '15 at 09:47
  • 1
    @alexolut So ask Stroustrup. What's the point of asking here? Is Bjarne a member? – user207421 Nov 09 '15 at 09:47

1 Answers1

6

This is the rationale for avoiding singletons from the same guideline collection:

I.3: Avoid singletons

Reason

Singletons are basically complicated global objects in disguise.

Example

class Singleton {
    // ... lots of stuff to ensure that only one Singleton object is created,
    // that it is initialized properly, etc.
};

There are many variants of the singleton idea. That's part of the problem.

My analysis of the intentions of the author:

Simpler is better. If disguising global objects in singletons doesn't solve the problems of global objects - like the guideline above implies - then there is no use in complicating the code by the use of the disguise.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Both singleton and global object are bad. But why authors recommend using of global object instead of singleton? That's the question. – αλεχολυτ Nov 09 '15 at 09:56
  • @alexolut it's just a matter of preference, global variables aren't that bad & can be useful in quite some places. it's just that they are diffcult to handle – Ankit Acharya Nov 09 '15 at 09:58
  • 3
    @alexolut the quoted guideline states that singletons are global objects in disguise. I would infer that means they don't solve the problems of global objects. It also states that singletons are more complicated than global objects. Simpler is better. – eerorika Nov 09 '15 at 10:02
  • 1
    And if you read the Exception to the I.3 guideline in this answer, it's *"You can use the simplest "singleton" (so simple that it is often not considered a singleton) to get initialization on first use"*, so the authors are hardly arguing against singletons *in earnest*. – Tony Delroy Nov 09 '15 at 10:13
  • How do the guidelines address using singleton where there should only be one instance of an object (like a physical resource)? – pooya13 Jul 18 '20 at 01:09