Saturday, December 25, 2010

An Old Programming Story

This is not so much a story about bad programming skill, or the inability to code algorithms, but more about short-sighted thinking, cut and paste garbage, and the high cost in readability that others have to pay when one cannot be bothered to think 2 steps ahead - which in my book, is worse than being unable to code an algorithm because it carries heavier downstream consequences.

Back around 1998 I was writing a system in an early J2EE framework called Silverstream for an FAA certification outfit. It was a small contract for the consultancy that I was working with, so when I needed a resource, they gave me a very green programmer - let's call him Yuri.

In this first phase of the project, we needed to implement a framework for data entry & validation in tabs, and wanted a visual clue on each tab to indicate the state of that tab's data, so in a street-light metaphor, I defined it thus:

I placed a small spherical gif next to the name of each tab, and changed the color of the sphere to correspond to the state of data in the tab.

Gray = empty or blank.
Yellow = has data that are not yet validated (against a server).
Red = has data that require correction before saving (failed validation).
Green = has data as saved on the server.

I asked "Yuri" to program the tabs with a validation object that had an instance variable of states mapping to these colors and could receive a message from various places to set itself. The fact that "receive message", "instance variable with access methods", and every other object-oriented term that came out of my mouth fell on completely deaf ears did not even cross my mind at the time. I thought that blank look was just the nervousness of his first real consulting assignment.

A couple of days later, I saw the result:
No objects, no colors, but a switch statement with lots of cut & paste in each case, and the following handy & intuitive abbreviations:
He started with an empty form, which was gray, so he simply used a one-character string (not bounded in any way - just a string that would break code if it was more than one character long), and selected 'g' for gray.
Then he retrieved some data from the server, which I had defined as green, but he had used up the 'g' for gray, so the next letter up, thinking with exasperating pinhole logic, was 'r' - <i>'r' now stood for green...</i> Thanks, "Yuri".
Then he modified some data on the tab, and implemented the 'y' for yellow.
But when he tried to save it & it failed validation, he needed to implement the red flag. Oops! (if you'll pardon the pun, because it was anything but OOP!) 'r' now stood for green, remember? - So he implemented red to be referenced as 'e'.

The end result? A system where:
Yellow='y'
Gray='g'
Red='e'
And... for the big prize in clear programming:
Green='r'

Lol

No comments:

Post a Comment