CodeSOD: Experience is Integral
Behind every code WTF is a process WTF. For example, Charles W was recently tasked with updating some file-handling code to match changes in the underlying file-format it parses. This is the C code which parses an integer:
if ((*p == '-' || ('0' <= *p && '9' >= *p)) && retCode == -256){retCode = 0;p = _tcsrev(p);if (*p == ' ')p++;for (i = 0; '0' <= *p && '9' >= *p; i++){retCode += (int)pow(10, (double)i) * ((int)*p - 0x30);p++;}if (*p == '-')retCode *= -1;}
This code does the basic, CS101 approach to parsing strings into integers, which is to go character by character and multiply by the appropriate power of ten. While no program should ever do this when there are perfectly fine built-ins, like wcstol, this isn't an utter disaster of a code block. Now, this is C and it's doing a bunch of pointer manipulation, it's certainly not safe code. Malformed inputs could almost certainly ruin your day here. It's bad and dangerous code.
But the code isn't the WTF. Note how this is very much the approach a college student or novice programmer might take? Well, fifteen years ago, Charles's employer hired on a college freshman doing a "work study" program. The student was "very good at programming", and so someone told them to implement the file parsing code and then let them go to town. No one checked in beyond "make sure it compiles and runs".
The result is a gigantic blob of code that looks like it was written by a talented but inexperienced college student. Wheels are reinvented. The difficult solution is usually preferred to the simple and clear one. It swings wildly from "overly modularized" to "it's okay if functions are 500 lines long", and "everything is documented" to "I forgot how to put in comments today".
And this is the code that they've been shipping for fifteen years. Charles is among the first to touch it. Which, hey, good on that mystery college student for writing code that was at least reliable enough that nobody had to deal with it before now. Shame on the employer, however, who tried to get important work done on the cheap by hiring inexperienced labor and providing them no supervision.
This is a case where, sure, the code isn't good, but the WTF is how that code got written. Charles adds, "That student is apparently working as a programmer somewhere...", and I hope that along the way they've found employers that could provide them guidance instead of just heaving them into the deep end and getting lucky.
[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!