During my recent five month stint at another company, I got into a debate with one of the long-time software engineers about code style. I was in the midst of working on a Linux video driver and had made some changes that I wanted him to have a look at. He made an off-hand comment about how I could feel free to change anything I wanted (he had written the driver I was working on). I responded that, while I'd like to change the way the code is formatted, I didn't see any point in doing so. It was just that the code was difficult to read.
One could hear the proverbial gloved hand smacking the face.
Alright, so it wasn't a knock-down, drag-out WWF style discussion, but I remembered the conversation yesterday while I was doing something I haven't done at Sun for quite some time; looking at code.
This is a great illustration of the topic we discussed (this code is freely available if you know where to look):
int ret, field, size, count, nbfrs; VidDevice *vid; VbiDevice *vbi; V4LDevice *pp; printk(KERN_INFO AMD_VERSION "\n"); pp = kmalloc(sizeof(*pp),GFP_KERNEL); if( pp == NULL ) return -ENOMEM; memset(pp,0,sizeof(*pp)); strncpy(&pp->name[0],DRVNAME,sizeof(pp->name)); pp->name[sizeof(pp->name)-1] = 0; pp->debug_level = debug; pp->irq = irq; lx_dev = pp; DMSG(1,"\n"); vid = &pp->vid; #ifdef CONFIG_PROC_FS lx_dev->proc = proc_mkdir(PROC_PATH,0); if( lx_dev->proc != NULL ) create_proc_read_entry("video",0,lx_dev->proc,proc_lxv4l2_vid_read,vid); #endif if( (ret=lx_init(pp)) != 0 ) return ret; if( (ret=vid_mem_init()) != 0 ) return ret; vbi = &pp->vbi; spin_lock_init(&vbi->lock); spin_lock_init(&vid->lock); vid->std_index = STD_SD_NTSC; vid->std_num = v4l_std_data[vid->std_index].num; vid->std_denom = v4l_std_data[vid->std_index].denom; v4l_default_capt_format(vid,&vid->capt); vid->capt_state = capt_state_idle; size = phys_bfrsz(vid->capt.sizeimage); count = v4l_max_buffers(size,vid); nbfrs = count - V4L_MIN_BFRS; if( nbfrs < V4L_MIN_BFRS ) nbfrs = V4L_MIN_BFRS;There is nothing functionally wrong with the above code. (Well, maybe there is, but that's neither here nor there). In fact, the engineer who wrote the code was highly respected, even by me. However, just because a piece of code is functional doesn't mean it's "good". I mentioned that the code was difficult to follow. Said engineer responded that code shouldn't read like poetry.
The glove was now on the other hand.
I didn't come right out and disagree, but in my opinion that could not have been more wrong. It goes back to the "functional code != good code" argument. The fact that it took me significantly more time to understand the code due to the formatting was my reason for why it was not good code.
If you had a close enough look at the code above, you undoubtedly noticed many statements written in one line that would traditionally comprise multiple lines (look for the if statements). The other notable "feature" is the utter lack of whitespace. The engineer's rationalization for this type of code "style" (or lack thereof) was that "real estate is king". The more code that can fit on the screen at a time, the better.
I find it difficult to argue with that type of logic. Fundamentally, he and I are on different planes of reality. Anybody that writes code for a living must eventually come to understand that nobody owns a piece of code forever. Somebody else will no doubt be responsible for maintaining or extending your code at some point in the future. Personally, I'd rather not have a curse on my house for generations to come because I wanted to be able to see an extra five lines of code without having to
scroll down. It seems to me this argument becomes even more salient when the code you write is visible by the entire world. The number of folks who would love to run into you in a dark alley late at night has now increased by several orders of magnitude. What is it that makes poetry inherently interesting? It's a combination of the words that are chosen, the way those words are put together, and the underlying message being conveyed. In fact, even the way those words are presented on paper lends itself to the enjoyment of the work.
With all this in mind, how is software different from poetry? Good code is a combination of the same factors. An appropriate choice of constructs, the method used to compile the constructs into something cohesive, and how the way the code is presented allows for a rapid understanding of the problem being solved are higher contributing factors to successful software than how many characters you can squeeze into a window.
Suffice it to say the denouement was less than satisfactory. I am no more likely to convince someone that software is very much like poetry than I am to swim the English Channel. I take solace in the hope that I am not the only one that believes in software as poetry.
Cheers,
David
