Skip to main content

CodeSOD: Single or Mingle

1 month 4 weeks ago

Singletons is arguably the easiest to understand design pattern, and thus, one of the most frequently implemented design patterns, even- especially- when it isn't necessary. Its simplicity is its weakness.

Bartłomiej inherited some code which implemented this pattern many, many times. None of them worked quite correctly, and all of them tried to create a singleton a different way.

For example, this one:

public class SystemMemorySettings { private static SystemMemorySettings _instance; public SystemMemorySettings() { if (_instance == null) { _instance = this; } } public static SystemMemorySettings GetInstance() { return _instance; } public void DoSomething() { ... // (this must only be done for singleton instance - not for working copy) if (this != _instance) { return; } ... } }

The only thing they got correct was the static method which returns an instance, but everything else is wrong. They construct the instance in the constructor, meaning this isn't actually a singleton, since you can construct it multiple times. You just can't use it.

And you can't use it because of the real "magic" here: DoSomething, which checks if the currently active instance is also the originally constructed instance. If it isn't, this function just fails silently and does nothing.

A common critique of singletons is that they're simply "global variables with extra steps," but this doesn't even succeed at that- it's just a failure, top to bottom.

[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
Remy Porter

Clean Energy Powered 40% of Global Electricity in 2024, Report Finds

1 month 4 weeks ago
The world used clean power sources to meet more than 40% of its electricity demand last year for the first time since the 1940s, figures show. The Guardian: A report by the energy thinktank Ember said the milestone was powered by a boom in solar power capacity, which has doubled in the last three years. The report found that solar farms had been the world's fastest-growing source of energy for the last 20 consecutive years. Phil MacDonald, Ember's managing director, said: "Solar power has become the engine of the global energy transition. Paired with battery storage, solar is set to be an unstoppable force. As the fastest-growing and largest source of new electricity, it is critical in meeting the world's ever-increasing demand for electricity." Overall, solar power remains a relatively small part of the global energy system. It made up almost 7% of the world's electricity last year, according to Ember, while wind power made up just over 8% of the global power system. The fast-growing technologies remain dwarfed by hydro power, which has remained relatively steady in recent years, and made up 14% of the worldâ(TM)s electricity in 2024.

Read more of this story at Slashdot.

msmash