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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

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

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 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

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

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

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»CodeSOD: Exceptional Control

    CodeSOD: Exceptional Control

    August 20, 2024

    Sebastian started a new job recently. Like a lot of “I started a new job,” stories, this one starts with a 1,000 line class definition. What’s notable about this one, however, is that most of that code is error handling. Now, you might think to yourself, “well, there’s nothing inherently wrong with loads of error handling, if the problem calls for it.

    This code is getting posted here. Do you think the problem calls for it?

    object Method1(MyOtherClass parameter)
    {
    try
    {
    if(parameter == null) throw new ArgumentNullException();

    //… 5 lines of code
    }
    #region catching
    catch(FormatException)
    {
    return null;
    }
    catch(InvalidOperationException)
    {
    return null;
    }
    catch(Exception)
    {
    return null;
    }
    #endregion
    }

    bool Method2(MyOtherClass parameter)
    {
    try
    {
    result = Method1(parameter);
    if(result == null)
    throw new Exception();

    // … 3 lines of code
    }
    catch(Exception)
    {
    return false;
    }
    }

    Names have been anonymized by Sebastian.

    We open with a mostly reasonable bit of code- if the input parameter violates our contract, we throw an exception. I don’t love exceptions for communicating contract violations- it’s much better if you can enforce that at compile time- but I won’t fault anyone for that. But gee, isn’t it a bit odd that we throw that exception inside of a try block?

    Oh, that’s because we catch the exceptions and return null. The fact that we catch multiple kinds of exceptions and just return null is already bad. It gets worse when we note that the last caught exception is Exception, the root of all exception classes.

    Normally, when we talk about the anti-pattern of using exceptions for flow control, we tend to think of them as spicy gotos, which is exactly what’s happening here. It’s just… we’ve removed the spice. It’s Minnesota Spicy Gotos- where the three grains of black pepper are too much zing. We’re jumping to the appropriate return statement. Which we could have just replaced the throw with a return. And you can’t even say that they’re trying to follow the rule of “only have one return”.

    The calling function makes the whole pattern worse. We invoke Method1, and if it returns null (that is to say, if it throws and catches its own exception), we… throw another exception. Which leads us to a return false.

    Sebastian tells us that this 1kloc class is about 70% error handling code, by volume, and as we can see, none of these errors need to happen.

    [Advertisement]
    Continuously monitor your servers for configuration changes, and report when there’s configuration drift. Get started with Otter today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWhat is EQ and why is it an important factor in improving sound?
    Next Article Get Paid for Your Art: Start a Graphic Design Business Today

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-40906 – MongoDB BSON Serialization BSON::XS Multiple Vulnerabilities

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    The tiny-but-mighty Geekom A8 mini PC can tackle big workloads – and it’s up to 22% off right now

    News & Updates

    Beyond the Reference Model: SimPO Unlocks Efficient and Scalable RLHF for Large Language Models

    Development

    How Can Your Website or Application Have More Sucess Deliverying Email Messages to Gmail, Yahoo and Other Email Providers X

    Development

    Unlocking the Potential of SirLLM: Advancements in Memory Retention and Attention Mechanisms

    Development

    Highlights

    News & Updates

    8BitDo and some of its killer retro-themed PC accessories are the latest to suffer from the U.S. tariff situation

    April 30, 2025

    8BitDo, makers of the wonderful Retro Mechanical Keyboard, has suspended shipments to the U.S. from…

    Vietnam-Based Hackers Steal Financial Data Across Asia with Malware

    April 4, 2024

    Xbox finally puts an end to this annoying issue where your video settings get disabled unexpectedly

    February 21, 2025

    This rugged and affordable smartphone has superpowers – but it’s not a tank!

    August 21, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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