Cut up the Area – The Day by day WTF

[ad_1]

Eva‘s co-worker wanted to strip the area title off an electronic mail handle, after which separate out the subdomains. The aim was that they wished to have the ability to match foo.bar.com and bar.com as being the identical area.

Now, for many of us, this would appear like a fairly simple software of a cut up perform. If we’re feeling like over-engineering it, we might escape a regex. (Please, do not use a regex for duties like this)

What we most likely would not wish to do is that this.

string GetDomain(string emailAddress, int howManyDots)
{
    string fullDomain = emailAddress.Substring(emailAddress.IndexOf('@') + 1);
    if (howManyDots == 0)
    {
        return fullDomain;
    }
    else if (howManyDots == 1)
    {
        attempt
        {
            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)
    { 
        attempt
        {
            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)
    {
        

        attempt
        {
            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 appears to be a case of somebody who did not perceive their instruments and who did not perceive the issue all that effectively. The truth that this requires hard-coded branches (and thus solely helps as much as three .s per area) actually highlights that. You’ll be able to see the primary glimmerings of “possibly I can do that with loops?” within the variable names- however they could not climb over that hill to complete the job.

Eva changed this with a a lot smaller perform that used Cut up.

[Advertisement]
Repeatedly monitor your servers for configuration adjustments, and report when there’s configuration drift. Get began with Otter right now!

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *