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: Yes, No, NO NO NO NO

    CodeSOD: Yes, No, NO NO NO NO

    July 31, 2024

    Mike was doing work for a mobile services provider. He found this in their code:

    private static YesNoType toYesNo(String isYes)
    {
    if (isYes != null)
    {
    if (isYes.equalsIgnoreCase(“Y”))
    {
    return YesNoType.fromString(“Yes”);
    }
    else
    {
    return YesNoType.fromString(“No”);
    }
    }
    else
    {
    return YesNoType.fromString(“No”);
    }
    }

    /**
    * @param isYes
    * @return
    */

    private static YesNoType toYesNo(boolean isYes)
    {
    if (isYes)
    {
    return YesNoType.fromString(“Yes”);
    }
    else
    {
    return YesNoType.fromString(“No”);
    }
    }

    /**
    * @param isYes
    * @return
    */

    private static String fromYesNo(YesNoType isYes)
    {
    if (isYes != null)
    {
    String resultStr = isYes.toString();
    if (resultStr.equalsIgnoreCase(“Yes”))
    {
    return (“Yes”);
    }
    else
    {
    return (“No”);
    }
    }
    else
    {
    return (“No”);
    }
    }

    /**
    * @param isYes
    * @return
    */

    private static boolean isYesNo(YesNoType isYes)
    {
    boolean isBroadbandUser = false;
    if (isYes != null && isYes.toString().equalsIgnoreCase(“Yes”))
    {
    isBroadbandUser = true;
    }
    return isBroadbandUser;
    }

    Look, I’m barely even interested in the functions here. They’re all varying degrees of bad, sure, but I really want to focus on YesNoType. I’ve seen people reinvent all sorts of wheels, but to reinvent a boolean in Java of all places, that’s novel. And like every reinvention, it looks like they reinvented it badly: it’s a stringly typed boolean, at least in terms of how it’s used here.

    That’s not to say that this code isn’t full of other horrors, but YesNoType is definitely the root of all evil here.

    But let’s dig in, anyway, starting with toYesNo(String). If the input string is “Y”, we store a “Yes” in a YesNoType. Otherwise, we store “No”. This means we can set a YesNoType with a “Y”, but when set, it always returns a “Yes”, which means we’re already in a space where we have inconsistent values.

    Now, toYesNo(boolean) tells us that we can’t construct a YesNoType directly off a boolean, which seems like a glaring mistake.

    fromYesNo, on the other hand, does a wonderful check to see if the YesNoType matches (ignoring case) a “Yes”, so that it can return a “Yes”. It almost seems like we shouldn’t need this at all if we had any sense, but we don’t.

    But the real capper is our isYesNo function, which converts our YesNoType into a boolean, aka the thing we should have been using the whole time. Because this code was clearly copy/pasted from somewhere less generic, we have a local variable called isBroadbandUser. A local variable we don’t need, as we could just do this as a single return. Or, better yet, we could have just used a boolean the whole time.

    At worst, we could have written a pair of functions bool fromString(String) and String fromBool(boolean) and called it a day. No new type. No weird string comparisons. A boolean is the simplest thing you can have, and to do it so wrong is an achievement.

    [Advertisement]
    Otter – Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous Article10 tech gadgets you actually need for college, according to recent grads
    Next Article Sikuli IDE or Eclipse/JAVA for windows based application?

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-47893 – VMware GPU Firmware Memory Disclosure

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Gmail’s new button makes using Gemini to reply to emails on Android a breeze

    News & Updates

    This new framework aims to finally set the standard for open-source AI models

    News & Updates

    Conoscete la prima distribuzione costruita intorno al kernel Linux?

    Linux

    What is Feature Testing: Advantages, and How To Test A Feature

    Development

    Highlights

    YugabyteDB 2.25 offers compatibility with PostgreSQL 15

    January 30, 2025

    Yugabyte has announced the release of YugabyteDB 2.25, which offers complete compatibility with PostgreSQL 15…

    termv – terminal iptv player

    February 12, 2025

    Outranking.io Review: Can It Really Improve SEO?

    June 13, 2024

    CISA Warns of Active Exploitation in Trimble Cityworks Vulnerability Leading to IIS RCE

    February 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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