Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The state of DevOps and AI: Not just hype

      September 1, 2025

      A Breeze Of Inspiration In September (2025 Wallpapers Edition)

      August 31, 2025

      10 Top Generative AI Development Companies for Enterprise Node.js Projects

      August 30, 2025

      Prompting Is A Design Act: How To Brief, Guide And Iterate With AI

      August 29, 2025

      Look out, Meta Ray-Bans! These AI glasses just raised over $1M in pre-orders in 3 days

      September 2, 2025

      Samsung ‘Galaxy Glasses’ powered by Android XR are reportedly on track to be unveiled this month

      September 2, 2025

      The M4 iPad Pro is discounted $100 as a last-minute Labor Day deal

      September 2, 2025

      Distribution Release: Linux From Scratch 12.4

      September 1, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Enhanced Queue Job Control with Laravel’s ThrottlesExceptions failWhen() Method

      September 2, 2025
      Recent

      Enhanced Queue Job Control with Laravel’s ThrottlesExceptions failWhen() Method

      September 2, 2025

      August report 2025

      September 2, 2025

      Fake News Detection using Python Machine Learning (ML)

      September 1, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Installing Proxmox on a Raspberry Pi to run Virtual Machines on it

      September 2, 2025
      Recent

      Installing Proxmox on a Raspberry Pi to run Virtual Machines on it

      September 2, 2025

      Download Transcribe! for Windows

      September 1, 2025

      Microsoft Fixes CertificateServicesClient (CertEnroll) Error in Windows 11

      September 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Single or Mingle

    CodeSOD: Single or Mingle

    April 9, 2025
    CodeSOD: Single or Mingle

    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:

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">SystemMemorySettings</span>
    {
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SystemMemorySettings _instance;
    
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SystemMemorySettings</span>()</span>
        {
            <span class="hljs-keyword">if</span> (_instance == <span class="hljs-literal">null</span>)
            {
                _instance = <span class="hljs-keyword">this</span>;
            }
        }
    
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SystemMemorySettings <span class="hljs-title">GetInstance</span>()</span>
        {
            <span class="hljs-keyword">return</span> _instance;
        }
    
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">DoSomething</span>()</span>
        {
        ...
            <span class="hljs-comment">// (this must only be done for singleton instance - not for working copy)</span>
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span> != _instance)
            {
                <span class="hljs-keyword">return</span>;
            }
        ...
        }
    }
    

    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.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous Article14 Best Free and Open Source Electronic Design Automation Tools
    Next Article Personalizziamo un po’ GNOME – Versione 2025

    Related Posts

    News & Updates

    Look out, Meta Ray-Bans! These AI glasses just raised over $1M in pre-orders in 3 days

    September 2, 2025
    News & Updates

    Samsung ‘Galaxy Glasses’ powered by Android XR are reportedly on track to be unveiled this month

    September 2, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    memethesis-cli creates memes from the terminal

    Linux

    CVE-2025-4484 – iSourcecode Gym Management System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-40909: Perl Threads Vulnerability Exposes File Operation Race Condition

    Security

    Microsoft AI Introduces Code Researcher: A Deep Research Agent for Large Systems Code and Commit History

    Machine Learning

    Highlights

    Development

    South Korea Accuses DeepSeek of Unlawful Data Transfers Amid AI Expansion

    April 24, 2025

    Chinese artificial intelligence startup DeepSeek has come under intense scrutiny from South Korean authorities for…

    Tempo – metronome for musicians

    July 23, 2025

    CVE-2025-1725 – WordPress Bit File Manager Stored Cross-Site Scripting Vulnerability

    June 3, 2025

    CodeSOD: Format Identified

    May 28, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.