The Previous Idiot (Also Me)

By Shamus Posted Thursday Nov 29, 2007

Filed under: Personal 27 comments

My post on programming the other day elicited a surprising number of responses. I had no idea that many coders visited this site. Compare this to my post on the Half-Life 2 fan commentary, which barely elicited a shrug. Considering that this is a blog ostensibly about tabletop games and videogames, you’d expect the opposite reaction.

There was a comment in there (but I can’t find it now, dangit!) where someone mentioned cringing at their early code and wanting to tear it out and re-write it. I’m going through that now with PHP. A couple of years ago I had to write some complex (to me) PHP for my job. Up until that point the only PHP I’d done was mucking about with WordPress themes. Simple stuff. But I suddenly found myself needing to write professional-grade code (instead of half-assed hobby code) which needed to interface databases, process complex form data, and do a bunch of other stuff I’d never had to deal with before. I needed to make my code readable to my eventual successor (or my future self) and I needed to idiot-proof the thing. As icing on the cake, the time budget for this project was more or less devised with the assumption that the guy writing the code knew what he was doing. Which I didn’t.

(Things like this happen needlessly in large companies, but I work for a small company and sometimes us little guys have to survive on our wits and duct tape. We can’t just run out and hire someone every time we have a gap in our knowledge. Sometimes this MacGuyver style development can be exciting, but sometimes it’s just annoying, and you’d rather just have the big pile of money required to do it right.)

I was both in a hurry and learning as I went, which is a great way to take newbie mistakes and set them firmly in stone. I got through it and finished the job, but now I look back on that old PHP code and I cringe. I could probably do the whole thing in half as many lines of code, make it more readable, and make it more re-usable, but there isn’t much of a justification for doing so. It works just fine and my time is needed elsewhere. But the fact that there is this snarl of ugly code under the hood bothers me whenever I use it.

Coders are used to the sensation of looking at a block of code and wincing, “What idiot wrote this mess?” It’s a rotten situation when that idiot is yourself.

By the way, if you want to nitpick my code and point out all of my crimes against C++, you can check out my terrain project. The final page gives the source code.

 


From The Archives:
 

27 thoughts on “The Previous Idiot (Also Me)

  1. QE says:

    There is no professional-grade code in our organisation. It’s all just been thrown together by me or the boss.

    I don’t often cringe at my own code but I have on occasion been able to marvel at a particularly elegant solution that was already in the codebase, only to realise that I wrote it and now wouldn’t have thought of it.
    I guess that demonstrates that far from improving, my coding is actually getting worse.

  2. Nothan says:

    Wow, QE, you are the reason I don’t see myself staying at my current job indefinitely. The vast majority of the code has been thrown together by the boss and I cringe whenever I write something significant because it has to be in the same style.

  3. Binks says:

    This is the main reason I never look back at my old program’s source codes, it’s hard to understand what in the world I was thinking. By far the worst offender in my list is an old space invader’s game written in Java back when I was still learning it which turned out to be 3000+ lines long. I could probably rewrite the whole thing with less bugs in 1000 lines now and so I’m almost afraid to look at the source and see what stupid mistakes tripled the length of it.

  4. Deoxy says:

    I didn’t comment before, but I’m a programmer, too, and yes, I’ve had that exact thought before (“What moron wrote this?!?!?!!? Oh… me. uh…”).

    One thing that drives at least a good many of those for me has been projects where the requirements get a complete overhaul when the project is 95+% finished…. at that point, you can’t go back and do it right, you just have to get it to work as best you can in the time available.

  5. Max Cantor says:

    I think looking back on an old project and cringing is a rite of passage for coders. Also, the seething need to go back and rewrite it from scratch is tempting, but indulging it is often folly. It’s definitely a function of every programmer’s desire to produce “the best” code that’s elegant and extensible and perfect, and of course, no code is ever good enough! Especially not the code written by that madman who came before you. What was he THINKING?

  6. Skjalm says:

    Since a large number of coders are also gamers and a lot of coders like to read blogs and comment it really isn’t that surprising that a lot of coders would read and comment on a blog about games ;-)

    Every coder I’ve ever met did, at some point, write ugly, horrible code that should be taking out in a dark corner of userspace and discreetly be piped to /dev/null.

    The thing to remember is that we all develop as coders and the important thing is to learn from our mistakes. Often a lot can be learned from looking at old code and rewriting it.

  7. Lebkin says:

    “My post on programming the other day elicited a surprising number of responses. I had no idea that many coders visited this site. Compare this to my post on the Half-Life 2 fan commentary, which barely elicited a shrug. Considering that this is a blog ostensibly about tabletop games and videogames, you'd expect the opposite reaction.”

    To me, it is very simple. I visit your site from work. Reading the occasional post is acceptable; watching movies is not. So the Half-Life 2 commentary might be wonderful and interesting, it isn’t as easy to appreciate from work.

  8. Nentuaby says:

    Yeah, I have that experience every… Oh… Four or five days.

    I have the rather unusual pleasure of working on a project which changes very fluidly- a web application on our own servers that we are update live, without worrying about point releases. It’s relatively young, and we have about a 6 month half life on our code.

    So I have the dubious pleasure of discovering my own hacks ALL THE TIME, but at least I can almost always indulge myself in ripping them out and rewriting!

  9. straechav says:

    Actually I enjoyed the Half Life fan post immensely. But the trouble with that post was that I followed the links and promptly spent next three or so hours watching the commentaries and laughing, thus forgetting to comment in the post at all… :p

  10. Tarlen says:

    Another programmer here – it seems that video and roleplaying games tend to sit well with programmers.

    I’ve also had your experience with PHP & Java. I wrote a game in Java about 6 years ago, and when I decided to “upgrade” it about 2 years ago, I honestly couldn’t figure out what half the code actually did.

    But in my experience, the worst experience is when you’re looking back at code you wrote the previous day. I have had times in one job when we were all working overtime to meet a deadline. One day I came in, looked at the code I had written the night before and thought “What was I thinking!”. Then deleted it all and started again.

    It really does show that overtime affects the quality of work. Of course, in my current job quality suffers because we have too many projects with tight timeframes, and not enough people to do the work.

    No matter where you go, there’s always something you wish you could change.

  11. scragar says:

    hacking and not commenting is a sure fire way to mess up something :P

    just wondering, but does anyone else build a die cause that prints out all of the information, line no and description(complete with error code) in the event of an error? it’s invaluble for spotting errors, just be sure to remove it before things go live, people hate being told: $i=5;$k=2;$j=1;$ar_val= Array(
    ‘stuff’
    );
    :P

  12. Tarlen says:

    Hmm… I suppose I should remove the website from my posts. There’s one annoying IT story…

    http://www.webhostingtalk.com/showthread.php?t=632124

  13. Miral says:

    Another coder here. Like Lebkin, I mostly read your site from work, so I can’t look at video here (although anything that looks interesting I’ll email back to my home address to remind me to look at later). I’ve still got some of the HL2 commentaries queued up for later viewing, but they looked interesting. Didn’t think a comment was necessary, though :)

    And yes, I frequently experience code horror (both for my own code and code made by other devs on the team, past and present). It’s just a normal part of development, as is the nagging desire to rip it out completely and rewrite most of it from scratch. That last occurs to me quite a lot since I’m often working on a codebase that goes back ~15 years…

  14. lxs says:

    Accept the fact that one is and always will be a noob in one’s own eyes. Appreciate and marvel that you had the determination to make it work at all!

    Besides, rewriting stuff often isn’t as easy as it looks. Old, heavily maintained code always accumulates cruft, but it’s probably all there for a reason. I just rewrote this sideeffect-ridden behemoth of a Java method into something much more functional, it took me an afternoon to write it and an extra couple of days to get it spot on.
    On the plus side, it’s now possible for a human to understand what’s going on. (I am of course a Higher Being rank 3)

    Personally my code is becoming more and more Lisp-y. I should actually learn Lisp one day. An intern showed me how to write a class definition in Lisp and well, cool doesn’t cover it.

  15. Barron says:

    Yeah, I cringe at every line I write at work. Because it’s all in VB.Net. <:(

    Seriously though, I know what you mean. The real problem for me is the constantly shifting requirements. Even the best written code eventually turns into a hideous snarl after enough revisions. We often talk about how software development should be more like engineering, but nobody asks an engineer to design a bridge that can span a 50 foot wide river, and then try to build it across a 100 foot river, and oh by the way, it needs to be a drawbridge. What? It’s already half-built? Can you make it a suspension bridge instead? And we decided we wanted to run trains across it, instead of cars. Also, we just decided we don’t like the building material you’re using, so….

    Ah well. I still enjoy it. Most days

  16. Alden says:

    I’ve had pretty much the same experience with Perl. I wrote a bunch of stuff for my website – not pretty, but it works. Then I had to write some Perl code for my job and realised I’d been doing it a bit sloppily, so I ended up rewriting most of them. :)

  17. Hirvox says:

    Programmers, like other people, do get better at what they do with enough practice. It’s completely natural. For the same reason you shouldn’t overengineer your code. You might not know how the customers’ needs will change in the future. Write what does the job now, write some tests to verify that it works and keep refactoring the code when you need to make some changes in that part of the code next time. The tests will make sure that you don’t accidentally break anything.

    That said, there are things that will help you minimize the amount of code you need to rewrite whenever you make some nontrivial changes. Personally, I recommend reading Agile Principles, Patterns & Practices in C#. Even if you don’t like Agile or use C#, many of the examples are generic enough to be applied to any object-oriented programming language.

  18. YaVerOt says:

    Even without ‘real’ coding experience, I recognize the obviousness of Test Driven Design (similar enough to what Hirvox says above). While I understand why it wasn’t used in the 50s-70s (systems more expensive than a programmer’s salary) or the 80s (640k was a limit we hit too soon & too often). When I went to college in the 90’s using those blazing fast 300Mhz machines (while being stuck at 100Mhz at home), the limitations holding back TDD didn’t apply, less so now that our consumer machines are rated at Ghz and most techies now realize that RAM is more important.
    Are entry (or near-entry) college classes still the “we’ll tell you how bad programming as a profession is”, or are they “Okay here’s the basics of language X, here are good resources for that language,and now I’m going to show you this technique called TDD that will keep programming fun, much like it was when you realized ’10 GOTO 10′ was a really elegant infinite loop.”

  19. Barron says:

    I started at Purdue in 2000, and they just jumped right into the content. We still had somthing like 80% atrition, so apparently tha wasn’t enough to make it “fun”. Personally, I really enjoyed that class

  20. Carra says:

    Barons comment reminds me of a quote:

    “If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilisation.” – Gerald Weinberg

    Programmer myself here. And a new one too, only got a job since 3 weeks ;)

  21. Yunt says:

    At work we have a piece of brand new, mission critical in-house software. It was engineered perfectly for it’s functionality but the design sucks from a user standpoint. It’s just incredibly unforgiving.

    Step one, look up your file, load it up and do some sanity checking…

    Step two, more sanity checking and manual interventions as necessary…

    Step three, more sanity checking and manual interventions as necessary…

    Step four, check records into database…

    The “manual intervention” screen is set to “Always on top” so it’s always in the way if you happen to need to look anything up. There is no “back” or “undo” button until just before Step Four when you can abort and start over. If you’ve typoed you just have to start over but it’s most recent incarnation is set up to remember your manual interventions so your mistakes, once made, are carved in stone. Always, between steps, you must hit the “next” button, despite the fact that there’s nothing else you could do anyway.

    It’s kind of a “first professional project” for our in-house team and it shows some of that same “learned it as I went” kinds of problems.

    I heartily recommend “The Inmates Are Running The Asylum” for anybody who works on, or with, software. Most software today is over-engineered and under-designed. It’s one thing to run a hundred error checks against a value but it’s a different problem domain entirely to decide why that value keeps coming up wrong to begin with. Keeping both perspectives in mind helps to keep code cleaner and less clever.

  22. Kris says:

    Yes. Another programmer here. :)

  23. spelley an says:

    I’m a web developer by trade and I’ve had the same experiences, where I’ve looked back at old code and winced. It’s funny, I was hired on at a company for C# and SQL Server databases and ended up having to teach myself PHP and MySQL on the fly as I went on my very first job.

    Now all I do is PHP and MySQL. Some of that early PHP code tho…*wince*

  24. Quadir says:

    nerdy programmers unite!

  25. I think programmers have a lot of spare cycles to spend reading blogs. And they tend to be into LoTR, HalfLife, etc.

    I’d be surprised if you did a poll and didn’t come up with at least 15% programmers.

  26. ryanlb says:

    I was teaching myself Lisp when I was in school and used emacs a lot. I think it’s incredibly cool, but didn’t get far enough with it to try any practical application of it.
    I also enjoyed Python a lot, for the little time I spent playing with it.

    I mostly do Java/JSP/HTML at work now, but I do a little php stuff on the side. Strangely, it tends to look a lot like my java code…

Thanks for joining the discussion. Be nice, don't post angry, and enjoy yourself. This is supposed to be fun. Your email address will not be published. Required fields are marked*

You can enclose spoilers in <strike> tags like so:
<strike>Darth Vader is Luke's father!</strike>

You can make things italics like this:
Can you imagine having Darth Vader as your <i>father</i>?

You can make things bold like this:
I'm <b>very</b> glad Darth Vader isn't my father.

You can make links like this:
I'm reading about <a href="http://en.wikipedia.org/wiki/Darth_Vader">Darth Vader</a> on Wikipedia!

You can quote someone like this:
Darth Vader said <blockquote>Luke, I am your father.</blockquote>

Leave a Reply

Your email address will not be published. Required fields are marked *