Writing

Feed Software, technology, sysadmin war stories, and more.

Wednesday, January 23, 2013

Come see some actual crap code

This discussion at Hacker News about a bunch of people picking on some random project got me thinking. They basically called it crap code. I don't think they've really seen crap code.

I can provide oodles of crap code. You know how? It's in my home directory. Why is it there? I wrote it. Here, feel the burn as I share one of my greatest abominations in the C language:

typedef struct {
        void    *next;
        int     datatype;
        int     state;
        char    dataptr;
}       stubtype;

As soon as you see "char dataptr" you should be going "oh no, what is she doing here"... so let's provide some more context.

        /* bunches of globals .. yes, these are necessary */
 
        stubtype        *stub, *laststub;

Now, given that, here's where it was actually used. I've un-indented it several levels here for the purposes of making it fit on my fixed-width page. In real life, this block was 3 hard tabs indented, since it was part of a huge read-from-file-and-parse block:

  /* if we have an exec command .. */
  if (!strcmp (execstr, "EXEC")) {
 
    /* get some memory */
    stub = malloc(8 + 128);
    stub->next = NULL;
 
    /* if this is #1, DON'T try to edit "before" */
    if (laststub != NULL) 
            laststub->next = stub;
    else    /* first one, set the pointer */
            ruleptr[rulenum] = stub;
 
    /* align exec over the new memory */
    exec = (exectype *) &stub->dataptr;
 
    /* this is an EXEC data type .. */
    stub->datatype = EXECDATA;
 
    /* with whatever state ... 0=off 1=on */
    stub->state = state;
 
    /* finally dump in the command to run */
    strcpy (exec->execcmd, tmpstr); 
  } /* "on exec" */

There are just so many things wrong with that code.

This excerpt is from the first "from scratch" program I ever wrote in C way back in 1996. For about a year prior to that point, I had only been using the language in small amounts to change things in other people's projects. The rest of the program is no better.

I wrote garbage code, and I even let other people see some of it. I think I shared this particular code with a couple of friends. It was messy and it had bugs and security holes and glaring omissions. In the 17 years since that point, I hope I've improved a little.

Here's my challenge: find your nastiest code and post it publicly. Let people see what kind of insanity flowed from your keyboard. Unless you were born knowing how to code flawlessly, you must have something worthy of that distinction.

Or, in the caveman language of the haters, "come at me bro".


January 24, 2013: This post has an update.