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

      Microsoft Graph CLI to be retired

      September 2, 2025

      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

      I asked AI to modify mission-critical code, and what happened next haunts me

      September 3, 2025

      Why you should delete your browser extensions right now – or do this to stay safe

      September 3, 2025

      Dolby Vision 2 comes with big upgrades – here’s which TVs get them first

      September 3, 2025

      This one small feature makes this travel charger my favorite for business trips

      September 3, 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

      Laracon AU 2025 Talk Titles Revealed

      September 3, 2025
      Recent

      Laracon AU 2025 Talk Titles Revealed

      September 3, 2025

      Handle ownership relationships between Eloquent models with Laravel Ownable

      September 3, 2025

      Stop Writing Bad Controllers: Laravel Custom Collections Transform Your Code

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

      Lenovo Legion Go 2 confirmed with Ryzen Z2 Extreme, 1200p OLED 144Hz display & 74Wh battery

      September 2, 2025
      Recent

      Lenovo Legion Go 2 confirmed with Ryzen Z2 Extreme, 1200p OLED 144Hz display & 74Wh battery

      September 2, 2025

      How to Open Ports in Firewall on Windows Server

      September 2, 2025

      Google TV Remote Not Working? 5 Quick Fixes

      September 2, 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

    I asked AI to modify mission-critical code, and what happened next haunts me

    September 3, 2025
    News & Updates

    Why you should delete your browser extensions right now – or do this to stay safe

    September 3, 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

    CVE-2025-3673 – Apache HTTP Server Remote Code Execution Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Ubisoft says you don’t own your video games, read the fine print

    News & Updates

    CVE-2025-42990 – SAPUI5 Cross-Site Scripting (XSS)

    Common Vulnerabilities and Exposures (CVEs)
    This AI Paper from ByteDance Introduces MegaScale-Infer: A Disaggregated Expert Parallelism System for Efficient and Scalable MoE-Based LLM Serving

    This AI Paper from ByteDance Introduces MegaScale-Infer: A Disaggregated Expert Parallelism System for Efficient and Scalable MoE-Based LLM Serving

    Machine Learning

    Highlights

    CVE-2025-30973 – Codexpert, Inc CoSchool LMS Deserialization of Untrusted Data Object Injection

    July 16, 2025

    CVE ID : CVE-2025-30973

    Published : July 16, 2025, 12:15 p.m. | 5 hours, 59 minutes ago

    Description : Deserialization of Untrusted Data vulnerability in Codexpert, Inc CoSchool LMS allows Object Injection. This issue affects CoSchool LMS: from n/a through 1.4.3.

    Severity: 9.8 | CRITICAL

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Hiding elements that require JavaScript without JavaScript

    April 30, 2025

    CVE-2025-5572 – D-Link DCS-932L Stack-Based Buffer Overflow Vulnerability

    June 4, 2025

    Xbox just made the best console version of Forza Horizon 5 yet — for the PS5 Pro

    April 24, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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