Eva‘s co-worker needed to strip the domain name off an email address, and then separate out the subdomains. The goal was that they wanted to be able to match foo.bar.com and bar.com as being the same domain.

Now, for most of us, this would seem like a pretty straightforward application of a split function. If we’re feeling like over-engineering it, we could break out a regex. (Please, don’t use a regex for tasks like this)

What we probably wouldn’t want to do is this.

string GetDomain(string emailAddress, int howManyDots)
{
    string fullDomain = emailAddress.Substring(emailAddress.IndexOf('@') + 1);
    if (howManyDots == 0)
    {
        return fullDomain;
    }
    else if (howManyDots == 1)
    {
        try
        {
            int rightMostDotPosition = fullDomain.LastIndexOf(".");
            string leftPartOfDomain = fullDomain.Substring(0, rightMostDotPosition);
            int secondRightMostDotPosition = leftPartOfDomain.LastIndexOf(".");
            return fullDomain.Substring(secondRightMostDotPosition + 1);
        }
        catch { return fullDomain; }
    }
    else if (howManyDots == 2)
    { 
        try
        {
            int rightMostDotPosition = fullDomain.LastIndexOf(".");
            string leftPartOfDomain = fullDomain.Substring(0, rightMostDotPosition);
            
            int secondRightMostDotPosition = leftPartOfDomain.LastIndexOf(".");
            string twoLeftPartsOfDomain = fullDomain.Substring(0, secondRightMostDotPosition);
            
            int thirdRightMostDotPosition = twoLeftPartsOfDomain.LastIndexOf(".");
            return fullDomain.Substring(thirdRightMostDotPosition + 1);
        }
        catch { return fullDomain; }
    }
    else if (howManyDots == 3)
    {
        

        try
        {
            int rightMostDotPosition = fullDomain.LastIndexOf(".");
            string leftPartOfDomain = fullDomain.Substring(0, rightMostDotPosition); 
            int secondRightMostDotPosition = leftPartOfDomain.LastIndexOf(".");
            string twoLeftPartsOfDomain = fullDomain.Substring(0, secondRightMostDotPosition); 

            int thirdRightMostDotPosition = twoLeftPartsOfDomain.LastIndexOf(".");
            string threeLeftPartsOfDomain = fullDomain.Substring(0, thirdRightMostDotPosition);
            int fourthRightMostDotPosition = threeLeftPartsOfDomain.LastIndexOf(".");
            return fullDomain.Substring(fourthRightMostDotPosition + 1);
        }
        catch { return fullDomain; }
    }
    return fullDomain;
}

This seems to be a case of someone who didn’t understand their tools and who didn’t understand the problem all that well. The fact that this requires hard-coded branches (and thus only supports up to three .s per domain) really highlights that. You can see the first glimmerings of “maybe I can do this with loops?” in the variable names- but they couldn’t climb over that hill to finish the job.

Eva replaced this with a much smaller function that used Split.

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

Remy Porter

Source link

You May Also Like

Car mods that are neither fast nor furious (32 Photos)

Adam Source link

The “worst” reasons people have decided to hook up (18 GIFs)

Jacob Source link

r/funny – What would you do?

This is a friendly reminder to read our rules. Memes, social media,…

John Deering for Nov 23, 2023 – John Deering, Humor Times

John Deering is chief editorial cartoonist for the Arkansas Democrat-Gazette, the state’s largest…