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

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 5, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 5, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 5, 2025

      In MCP era API discoverability is now more important than ever

      June 5, 2025

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025

      Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

      June 5, 2025

      Xbox just quietly added two of the best RPGs of all time to Game Pass

      June 5, 2025

      7 reasons The Division 2 is a game you should be playing in 2025

      June 5, 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

      Mastering TypeScript: How Complex Should Your Types Be?

      June 5, 2025
      Recent

      Mastering TypeScript: How Complex Should Your Types Be?

      June 5, 2025

      IDMC – CDI Best Practices

      June 5, 2025

      PWC-IDMC Migration Gaps

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

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025
      Recent

      Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

      June 5, 2025

      Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

      June 5, 2025

      Xbox just quietly added two of the best RPGs of all time to Game Pass

      June 5, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Static State

    CodeSOD: Static State

    April 17, 2025

    Today’s Anonymous submitter was reviewing some C++ code, and saw this perfectly reasonable looking pattern.

    class SomeClass
    {
    public:
    	void setField(int val);
    	int getField();
    }
    

    Now, we can talk about how overuse of getters and setters is itself an antipattern (especially if they’re trivial- you’ve just made a public variable with extra steps), but it’s not wrong and there are certainly good reasons to be cautious with encapsulation. That said, because this is C++, that getField should really be declared int getField() const– appropriate for any method which doesn’t cause a mutation to a class instance.

    Or should it? Let’s look at the implementation.

    void SomeClass::setField(int val)
    {
    	setGetField(true, val);
    }
    
    void SomeClass::getField()
    {
    	return setGetField(false);
    }
    

    Wait, what? Why are we passing a boolean to a method called setGet. Why is there a method called setGet? They didn’t go and make a method that both sets and gets, and decide which they’re doing based on a boolean flag, did they?

    int SomeClass::setGetField(bool set, int val)
    {
    	static int s_val = 0;
    	if (set)
    	{
    		s_val = val;
    	}
    	return s_val;
    }
    

    Oh, good, they didn’t just make a function that maybe sets or gets based on a boolean flag. They also made the state within that function a static field. And yes, function level statics are not scoped to an instance, so this is shared across all instances of the class. So it’s not encapsulated at all, and we’ve blundered back into Singletons again, somehow.

    Our anonymous submitter had two reactions. Upon seeing this the first time, they wondered: “WTF? This must be some kind of joke. I’m being pranked.”

    But then they saw the pattern again. And again. After seeing it fifty times, they wondered: “WTF? Who hired these developers? And can that hiring manager be fired? Out of a cannon? Into the sun?”

    [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleFOSS Weekly #25.16: Ubuntu 25.04, Fedora 42, ParticleOS and a Lot More Linux Stuff
    Next Article Model Performance Begins with Data: Researchers from Ai2 Release DataDecide—A Benchmark Suite to Understand Pretraining Data Impact Across 30K LLM Checkpoints

    Related Posts

    News & Updates

    Google’s DeepMind CEO lists 2 AGI existential risks to society keeping him up at night — but claims “today’s AI systems” don’t warrant a pause on development

    June 5, 2025
    News & Updates

    Anthropic researchers say next-generation AI models will reduce humans to “meat robots” in a spectrum of crazy futures

    June 5, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Do this first in Atomfall before freeing Dr. Garrow — you can thank me later for making it so much easier

    News & Updates

    Help The Site: Suggest an Active Linux Distribution

    Development

    How to Get Your First SaaS Customers

    Development

    CVE-2025-1305 – NewsBlogger WordPress CSRF Remote Code Execution Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    EMOTION: Expressive Motion Sequence Generation for Humanoid Robots with In-Context Learning

    January 24, 2025

    This paper introduces a framework, called EMOTION, for generating expressive motion sequences in humanoid robots,…

    How to get Google’s new Pixel 9a for free

    April 11, 2025

    Kontainer is a GUI tool to manage Distrobox containers

    May 20, 2025

    Away From the Keyboard: Ariel Hou, Staff Engineer

    January 27, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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