CodeSOD: "Performant" ""Development""
by Ellis Morning from The Daily WTF on (#1S6VT)
Good intentions are never enough. If they aren't coupled with the wisdom to follow through properly, they can result in a horrible mess. Today's Anonymous submitter has learned this the hard way:
I'm working on this particular 500k LOC monster for a few years now, and I consider this code to be a "representative function". It's a performance-critical server application crunching huge amounts of data through its bowels, and its development started around 2001 on a desktop computer. For this reason, the original developer chose to '"'develop'"' with performance being the primary concern (see, I put the quotes into quotes to let you know that the original developer was very creative about "developing"; he "made things work", you know).
He was very agile and customer-driven ("Yeah, refresh the page a few times, and if the right thing comes out, I'll put that live on all systems."). He was environment-aware (he did variable recycling). He kept the code clean and simple ("char*" and "int*" is all you ever need). He wrote easy-to-use interfaces (query parameters are mostly bit-flags, unless you query with a few special values like "format=100" or "format=3", which do something very useful). He wrote an easy-to-use line-based output format (sometimes switching between "\n" and "\r\n", and the lines themselves even had context-sensitive formats, alternating between pipe- and semicolon-separated lists). Users didn't have to cope with errors (if the first line of the output was "0" or "-1", something went wrong). [He] even showed different results in consecutive requests if there were too many results to fit into the size-limited list, so the user had the chance to see everything [that] might be of interest to him!
int GetAgeFromBirth(time_t nBase, int nBirth){ tm tms;#if _MSC_VER >= 1400 localtime_s(&tms,(time_t*)&nBase);#else tms = *localtime((time_t*)&nBase);#endif tms.tm_year+=1900; tms.tm_mon+=1; char strConv[8]; sprintf(strConv,"%06d",nBirth); int nD,nM,nY; if(sscanf(strConv,"%2d%2d%2d",&nD, &nM, &nY)==3){ nY+=2000;if(nY>tms.tm_year)nY-=100; int nRet=tms.tm_year-nY; if(tms.tm_mon<nM || (tms.tm_mon==nM && tms.tm_mday<nD)) nRet-=1; return nRet; } return 25;}[Advertisement] Otter, ProGet, BuildMaster - robust, powerful, scalable, and reliable additions to your existing DevOps toolchain.