Article 40Y03 CodeSOD: My Condition is Complicated

CodeSOD: My Condition is Complicated

by
Remy Porter
from The Daily WTF on (#40Y03)

Anneke's organization is the sort of company where "working" takes precedence over "working well". Under-staffed, under-budgeted, and under unrealistic deadlines, there simply isn't any emphasis on code quality. The result is your pretty standard pile of badness: no tests, miles of spaghetti code, fragile features and difficult to modify implementations.

Recently, the powers that be discovered that they could hire half a dozen fresh-out-of-school developers on the cheap, and threw a bunch of fresh-faced kids into that mountain of garbage with no supervision. And that's how this happened.

XmlNodeList nodeList = NodeListFromSomewhere();do{ // A short operation which is very cheap to perform}while (index < nodeList.Count && ( (isPolicy && (nodeList.Item(index).CloneNode(true).SelectSingleNode(xpathPolicyNo, XmlNsMngr).InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "") == policyNumberSystem) && (nodeList.Item(index).CloneNode(true).SelectSingleNode(xpathDocumentType, XmlNsMngr).InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "").ToUpper().Trim() != packageContent) ) || (!jePolica && (nodeList.Item(index).CloneNode(true).SelectSingleNode(xpathOfferNumber, XmlNsMngr).InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "") == offerNumber) && (nodeList.Item(index).CloneNode(true).SelectSingleNode(xpathDocumentType, XmlNsMngr).InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "").ToUpper().Trim() != packageContent) && (nodeList.Item(index).CloneNode(true).SelectSingleNode(xpathPolicyNo, XmlNsMngr).InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "") == "") ) )

While Anneke hasn't had a chance to profile this code, they're almost certain that the body of the loop is cheaper to evaluate than the loop condition, and given the number of regexes chained on, and nodes getting cloned, that's not implausible. When Anneke found this code, whitespace wasn't used to make it readable- it was just one extremely long line of code. Anneke's only contribution to this section of code was to improve the indenting, because as mentioned before: if it works, it ships, and the code, as it stands, works.

raygun50.png [Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more. TheDailyWtf?d=yIl2AUoC8zAXIwvtj4Y0do
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