If you’ve dealt with code, you’ve almost assuredly run into a ball of code you’d rather not deal with at all, but you have to anyway. If you’ve done it long enough, you’ve probably been the progenitor of the same for someone else to deal with. You might be the kind of person who actually wrote clean, readable code from birth, but I think you’re likely to be the exception, and you’ve still probably had to deal with the efforts of the rest of us. So, I figure we can share our nigthmares here.
I’ll share one I had to deal with and one I created to start us off. It kind of neatly bookends my tenure at one job.
The one I had to deal with:
When I was a fledgling Unix admin, I ran into problems and irregularities (a bunch of fucking bugs) in a Perl script that our CTO had written that in the end managed mail routing and aliases on all of the 4000 or so systems that our company ran/rented using our standard Linux image. I got access to the unencrypted file shortly before he left the company, and endeavored to resolve some of them.
It started with the worst variable names. and never got any better. IIRC sequentially $A-$NN (yes, all scalars), in less than 200 lines of intentionally bizarrely written code. No comments whatsoever. When talking to another employee, I found out that the previous CTO had admitted he himself had no idea how it worked anymore. I slogged through it to where I had about half the code figured out and commented before I found out that we were going to ditch everything about the old system, anyway. I still dealt with other code that fool created, but none was as nasty as that.
The one I created:
The same company had no money and a constant backlog of DNS tickets that never ended. Since I was the guy who handled most of those tickets, I was tasked to write the customer facing DNS control panel they couldn’t afford to buy that would interface with the APIs of their existing CRM system. I had mostly used Perl and/or Bash in most of my work at this job, but the CRM was written in PHP, and its examples for authenticating were written in PHP. Most of my experience with PHP up to this point amounted to looking at the docs and explaining to customers “this is how you’re doing it wrong” and providing enough of a code snippet to demonstrate the correct way to do it. But, if my customers could do it, how hard could it be?
So, I started to actually learn PHP while writing a reasonably secure, functional DNS control panel. IIRC, it was around 4000 lines of code with comments, etc. in the end. When it was mostly functional and around 2000 lines, a co-worker of mine who was actually a pretty good PHP developer took a look at look at it, corrected a bunch of formatting and said “I cleaned it up some, but it looks like it was written by five different crazy people”. However, even he was kind of impressed about how well it worked and what it did. I switched between OOP and procedural styles where it felt easier for me when dealing with that idea. But in the end, I was the idiot savant coder version of James Joyce. Clever, but hard to interpret. Even though it was littered with comments and descriptive variable names, I still had to kind of wrap my head around how it did things when I needed to return to it and work it through new issues and fix things. One coding style, or having the sense to break the silly thing into modules would have gone a long way.
When I left the company, I checked the docs for the next release of their CRM system, noticed the authentication system was going to change and told them “Don’t upgrade your CRM without contacting me first so I can re-write the auth system on the DNS control panel.” Of course, they didn’t remember that, upgraded it, and broke it, and apparently never contacted me. They cycled through a couple of admins that couldn’t fix it, and then happened to hire a friend of mine who had the good sense to contact me, who I helped fix it in about 4-5 messages after he got to looking at the code (it also didn’t hurt that he’s smarter than I am, but he still had to ask to be sure).
So, any steaming piles of code that other Dopers have had to fix or have embarrassingly left around for others to deal with?