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: Black Letters

    CodeSOD: Black Letters

    July 1, 2024

    Johannes started debugging an application, and decided he needed to “share his pain”.

    Here, we’re presented with a simple problem: convert a number in the range [0-25] to a letter [A-Z]. Many people would solve this with an array of letters as a lookup table. If they’re clever, they’d leverage the character encoding and do some arithmetic.

    Or, they could just have a gigantic switch:

    private function getLetter($value, $type = 0){
    $data = ”;

    if($type == 0){
    switch($value){
    case 0: $data = ‘A’; break;
    case 1: $data = ‘B’; break;
    case 2: $data = ‘C’; break;
    case 3: $data = ‘D’; break;
    case 4: $data = ‘E’; break;
    case 5: $data = ‘F’; break;
    case 6: $data = ‘G’; break;
    case 7: $data = ‘H’; break;
    case 8: $data = ‘I’; break;
    case 9: $data = ‘J’; break;
    case 10: $data = ‘K’; break;
    case 11: $data = ‘L’; break;
    case 12: $data = ‘M’; break;
    case 13: $data = ‘N’; break;
    case 14: $data = ‘O’; break;
    case 15: $data = ‘P’; break;
    case 16: $data = ‘Q’; break;
    case 17: $data = ‘R’; break;
    case 18: $data = ‘S’; break;
    case 19: $data = ‘T’; break;
    case 20: $data = ‘U’; break;
    case 21: $data = ‘V’; break;
    case 22: $data = ‘W’; break;
    case 23: $data = ‘X’; break;
    case 24: $data = ‘Y’; break;
    case 25: $data = ‘Z’; break;
    default: $data = ‘XYZ’; break;
    }
    }else{

    }

    return $data;
    }

    Honestly, though, the switch isn’t the worst part of this. I’d argue the gigantic switch is bad, but not a WTF. No, there are two worse things.

    First, when the number is outside of the range, it returns ‘XYZ’. That’s not a letter. That’s a multi-character string. I’d rather get a null, or a zero, or a thrown exception. This is weird, unexpected, and potentially breaks code that foolishly assumes getLetter returns a single character string.

    Second: what the hell is type? Here we have a parameter that just… makes the function not work? Were they hoping to one day make this multilingual (and were they planning to make switches for every new language)? Did they just forget why they accepted the parameter and decided this was the best they could do?

    As it turns out, many functions accept a type parameter. Some do something with it. Some don’t. No one knows why.


    [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 ArticleElden Ring’s Seamless Co-op mod got updated for the DLC, so have fun exploring Shadow of the Erdtree in barrier-free multiplayer
    Next Article Archman Linux – Arch-based distribution

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4610 – WordPress WP-Members Membership Plugin Stored Cross-Site Scripting Vulnerability

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Ten Wild Examples of Llama 3.1 Use Cases

    Development

    Advanced Array Methods in JavaScript: Part 3

    Development

    AI Engineering is the next frontier for technological advances: What to know

    Development

    How the 2024 US presidential election will determine tech’s future

    Development

    Highlights

    Databases

    Near zero-downtime migrations from self-managed Db2 on AIX or Windows to Amazon RDS for Db2 using IBM Q Replication

    June 11, 2024

    Db2 is an IBM relational database that supports large-scale transactional and analytical workloads. Amazon Relational…

    CVE-2025-32433: Unauthenticated RCE Vulnerability in Erlang/OTP’s SSH Implementation

    April 20, 2025

    Revolutionizing Financial Marketing with Artificial Intelligence (AI)

    November 15, 2024

    Two of the best-looking laptops of 2025 landed on my desk, so here’s a photoshoot

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

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