Article 6C7Z9 CodeSOD: Split the Domain

CodeSOD: Split the Domain

by
Remy Porter
from The Daily WTF on (#6C7Z9)

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) { //abc.def.ghi.com. should return def.ghi.com try { int rightMostDotPosition = fullDomain.LastIndexOf("."); string leftPartOfDomain = fullDomain.Substring(0, rightMostDotPosition); //abc.def.ghi int secondRightMostDotPosition = leftPartOfDomain.LastIndexOf("."); string twoLeftPartsOfDomain = fullDomain.Substring(0, secondRightMostDotPosition); //abc.def int thirdRightMostDotPosition = twoLeftPartsOfDomain.LastIndexOf("."); return fullDomain.Substring(thirdRightMostDotPosition + 1); } catch { return fullDomain; } } else if (howManyDots == 3) { //abc.def.ghi.com. should return abc.def.ghi.com try { int rightMostDotPosition = fullDomain.LastIndexOf("."); string leftPartOfDomain = fullDomain.Substring(0, rightMostDotPosition); //abc.def.ghi int secondRightMostDotPosition = leftPartOfDomain.LastIndexOf("."); string twoLeftPartsOfDomain = fullDomain.Substring(0, secondRightMostDotPosition); //abc.def 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.

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments