A million lines of bad code

Original author: David Robinson
  • Transfer
“No pain, no gain,” as ancient Eastern wisdom says. And even if the wisdom is neither ancient nor eastern, for me personally the most valuable life experience was most often the most painful. A recent post by David Robinson, a graduate student at Princeton University programming, devoted to code reviews, has not only raised the important issue of everyday life for everyone who, by will (or not) has to pass on his experience to others. The original text was relatively “toothless”, however, the post ceased to be languid after John Carmack appeared in the comments.

This is a story about a bad code that your humble servant once wrote.

At one of the first university courses, I wrote a Java program that was supposed to read a file weighing 6 MB per line (this file was a bacterial gene in FASTA format). My code looked like this:

BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
String text = "";
while( ( line = reader.readLine() ) != null ) {
    text = text + line;
}

Building a string using a series of concatenations in this way is extremely inefficient - without exaggeration, it took me about 40 minutes to read a file ( since then I have learned several better ways ). The most important thing is that after reading the file, the entire remaining algorithm in the program worked for 10 seconds. For two days I worked like this: I made changes to the code, ran the program and managed to watch the whole LOST episode before the program finished execution. “Damn, the twelfth line is a mistake! Again, all in a new way ... "

After many re-launches, I finally thought, “There must be a better way to do this.” I found out that it is possible to write a loop in Perl that can take a gene in less than one second (at the same time I was able to program in Perl no better than in Java - I was just lucky). So, I sat down and wrote a Perl script that read the file, collected it on one line, and output it. Then I made my Java program call the Perl script through the command line, capture the output and save it to the string.

If I had saved this program, I would have printed its source code and hung it in a frame on the wall to remind myself more often that I would never scold anyone for the “bad” code he wrote.

To tell about this case from my life I was inspired by a fresh comic strip from XKCD about “code quality”:

image

You can’t call a comic book “evil” - an experienced programmer does not give any useful advice, only draws hyperbolic analogies. But what caught my eye most of all was the patient’s response - "Well, I read the coding style guide." Will a living person react so if someone is so rude to him? Doesn't the answer look like “Okay, this was the last time I showed you my code”, or even “Okay, I think I should get into programming”?

The problem is present in scientific research. There are many reasons why scientists publish their articles without attaching code to them (and they are understandable), but the first item on their list is usually embarrassed: "my code is too ugly to give it to someone." Those who seek to shame others for the code do not help too much in this matter!

A famous piece of advice for budding writers reads: “Each of us holds a million bad lines. Before this million spills out, trying to write something good is useless. So go and write! ” I remembered him when I read the discussion that erupted about the above XKCD:

The next time you, an experienced programmer, want to shame someone for his code, try mentally returning to your own million lines of code. Imagine that someone condemned you in the same way as I myself above. Would you continue to ask for help? Would you make your way into a million lines?

Comment by John Carmack


Do not confuse the words “the idiot who wrote this code” and “this shit code” - there is a significant difference between them. The comic is about the last. I suppose that this was done consciously. An attack on the author is unlikely to bring any benefit, but there should be the ability to strictly criticize the code itself. In real life, many authors cannot separate themselves from their work, but the ability to do this is a valuable skill.

Left to their own devices, most people demonstrate an impenetrable ability to ignore their flaws, and this harms their professional growth. A little shame often becomes a positive motivation. I am ashamed of the huge amount of the code that I wrote over the past year. I have thoughts in my head about the reasons why everything turned out the way it turned out - some of them are excuses, but some of them are pure “WTF did I even think when I wrote this?” And if you don’t feel a little guilt for your recent work, then it is quite possible that it will be useful for you if someone points out your problems - in the sense that this person will be able to break through your “defense mechanisms”.

I would be happy if someone “buried” in all the code that I wrote for the Oculus Mobile SDK. I am sure that for the most part I will become this reader sooner or later myself, and for the most part I will only nod my head and agree with what is written; however, I am sure that I can learn something useful from this, and this will have a beneficial effect on my future work.

>> Are you in Oculus doing a review code for each commit?

Not. We have some progress in this direction; I will be interested to see what happens. The pressure of the pace of work on a startup is still greatly affecting us.

>> Your commentary on the article looks like a macaque rolled on the keyboard, and then you just went for a look auto-correcting ... well, it sounded not too useful, right? I think that you confuse excessive rigidity with serious criticism. Noticed that my last phrase is not so offensive, and at the same time brought more sense than the first?

Usually tactful and helpful comments work best, but there are times when a certain minimum of “activation energy” is needed for the comment to have an effect on the person.

>> Have you done code reviews written by other developers in id in the past?

Informally, and then that was enough for me. I don’t like doing code reviews; perhaps for this reason I appreciate criticism.

Even though John admits that he partly does not shy away from strict comments, he is far from Linus Torvalds, who generously sprinkles his letters with personal insults, and his speeches with phrases like “You are not interested in me, I'm worried about technology and the core - that’s what is for me really important ”and the claims that “ no one will hear you if you are going to be gentle. ”

Also popular now: