An Array of Parameters – The Daily WTF

Date:

Share:

Andreas found this in a rather large, rather ugly production code base.

private static void LogView(object o)
{
    try
    {
        ArrayList al = (ArrayList)o;
        int pageId = (int)al[0];
        int userId = (int)al[1];

        
    }
    catch (Exception) { }
}

This function accepts an object of any type, except no, it doesn’t, it expect that object to be an ArrayList. It then assumes the array list will then store values in a specific order. Note that they’re not using a generic ArrayList here, nor could they- it (potentially) needs to hold a mix of types.

What they’ve done here is replace a parameter list with an ArrayList, giving up compile time type checking for surprising runtime exceptions. And why?

“Well,” the culprit explained when Andreas asked about this, “the underlying database may change. And then the function would need to take different parameters. But that could break existing code, so this allows us to add parameters without ever having to change existing code.”

“Have you heard of optional arguments?” Andreas asked.

“No, all of our arguments are required. We’ll just default the ones that the caller doesn’t supply.”

And yes, this particular pattern shows up all through the code base. It’s “more flexible this way.”

[Advertisement]
Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.

Source link

Subscribe to our magazine

━ more like this

How to Explore Ludington, Michigan

This is a guest post by Raimee Iacofano from Raimeetravel. Growing up in Michigan, Ludington was THE place to be. Every summer, my family would...

How to Protect Your Company From Deepfake Fraud

Opinions expressed by Entrepreneur contributors are their own. In 2024, a scammer used deepfake audio and video...

Emails Sent to PostSecret – PostSecret

A friend from college told a bunch of us how once, he and some friends had dropped acid and then went to Disneyland. They...

Taylor Swift’s Ralph Lauren Engagement Dress

While each product featured is independently selected by our editors, we may include paid promotion. If you buy something through our links, we may...

Do anti-aging hair care products really work? Episode 130

Author: Randy Schueller Published: April 26, 2016 Hair and skin have some things in common but there’s one big difference: skin is alive and responds to...