Friday, 15 April 2011

Pattern Recognition

We were working nights and weekends, trying to put out a release, when I was assigned a bug in the patterning system.

This was deliberate, of course. Bashir had scheduled quarterly releases for one reason and one reason only: he wanted to show deliverables on his resume. In order to make sure that these were attainable, he deferred all new development work in favour of a less risky and time-consuming approach: working through the bug list. Bash would choose a number of defects and declare that they had to be completed for the release. As the list reduced in size he would top it up to ensure that we worked through through the weekends.

I'm not making this up. Bash was heard to say, in front of the entire team that he liked to burn developers out and then hire new ones for cheap. When this elicited complaints, he arrived at a staff meeting with a three inch stack of documents. "You know what these are?" he asked. "These are all the resumes I've received from people who want your jobs. And that's just this week." He said it with a smile on his face.

During one of these death march releases I was assigned the defect in the patterning system. "An easy one for you, Pike." I didn't like the patronizing tone, but I didn't want to reinforce my growing reputation as a troublemaker, so I took it on without complaint. It was, after all, my job to fix these things.

I spent more than a full day trying to determine what this 'easy' problem actually involved.

Our software allowed users to set a line style with which two dimensional shapes would be drawn. Dashes lines, dotted lines, dash-dot-dash, crosses--there were at least a dozen options.

In addition to that, users were able to provide cel patterns in two different formats, in which case the shapes would be drawn as a continues string of cels.

All of that worked fine, and I couldn't find a problem that needed fixing. Eventually my manager Eric managed to communicate to me that it was possible to have a cel and a line style active at once. In this case, the cels that constituted the objects would themselves be drawn with the selected line styles.

This worked fine for one for the cel formats, but the other yielded results that were... undesirable. At some point it the graphics engine would take a left turn and the shape would unravel into something unrecognizable.

Nobody had analyzed the bug before it was assigned to me and I had to grill the dev manager, Eric; a product manager, Kerry; and the architect, Abhiraj, who had written the code in the first place. Nobody wanted to help me; they were all working weekends as well, and everyone was tired and surly. But, finally, I understood what the issue was and I was ready to plunge into the codebase.

In this user interface was a swiss cheese, riddled with gotos and encrusted with ten years' worth of black mold. The rendering system was a plate of spaghetti, in which data structures were transformed and reconfigured endlessly as they were passed through an finite state machine which would calculate which part of the world to display, transform the data as appropriate, pipeline it and then flush it to the screen by passing it through a succession of abstracting libraries. It was an unstructured mess that had grow beyond the capacity of any single developer to understand. Changes were hacked on, because there was no other way to improve it, so the the longer it remained the worse it got.

The finite state machine had only one comment in its thousands of lines of code:

// state machine.

I changed it to

// 5t4t3 m4ch1ne

and wondered what the fuck I was going to do next.

It was probably the most difficult code I have ever traced through in my life. The cel pattern code for the two different formats diverged and converged in many different places and I had to step through the execution of the program dozens and dozens of times (thank goodness for the debugger!) in order to have any idea of what was going on.

Eventually, after beating my head against the code for three days, I had it narrowed down to a single function. Two dozen lines of inscrutable code. Perhaps, if I wasn't on the verge of collapse, I would have been able to figure out what was wrong, but I just could not work it out. Source control showed me that it had been written by Raj, so I showed it to him and asked what it did.

Raj's eyes bugged out as he regarded the code. Then they narrowed to slits. "I'm far too busy," he said. "Any idiot should be able to work that out."

Eric saw that I was toast, so he let me handball the bug on to Sam, another junior. I showed Sam what I'd found and within an hour or so he nailed it. It was, indeed, somewhere in the twenty four lines I had identified for him.

Once Sam checked the fix in, Bash called everybody together to tell us the good news. "Sam here has, in one day, solved one of the most difficult bugs in the system. It's been in there for nearly ten years, and nobody has come anywhere close until Sam put his brain to it."

Sam wasn't one to grab credit. Any for of attention would turn him scarlet and rob him of his powers of speech, and I'm sure he'd have said something if he was at all capable. Bash, on the other hand, was very keen on performance measurement and metrics, and he knew exactly how much time and effort I'd spent tracing that defect through the worst the codebase had to offer.

Bashir clapped Sam on the back, smiled at me, and went off to his office to write up an Employee of the Month commendation for Sam. I believe that, in conjunction with Sam's other commendations, it actually resulted in cash bonus. Sam certainly deserved it; he was every bit the uncomplaining, efficient and conscientious employee that I was not.

I went back to my desk and spent a half an hour polishing up my resume. It was the weekend, after all.


  1. You've successfully made me wish for something very, very bad to befall Bashir in upcoming installments.

    Keep writing, I'm enjoying very much.


  2. Er... I did refuse his friend request on facebook. Does that help?

  3. Ha ha, cut him to the bone, I'm sure.