📜 ⬆️ ⬇️

A million lines of bad code

“No pain, no gain,” as the ancient Eastern wisdom says. And even if the wisdom is not ancient and not Eastern, for me personally the most valuable life experience was most often the most painful. The recent post of David Robinson, a graduate student in programming at Princeton University, a dedicated review code, not only raised the important question of everyday life for everyone who would (or not) have to pass on their 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 bad code that your humble servant once wrote.

In one of the first courses of the university, I wrote a Java program that was supposed to read a file weighing 6 MB per line (this file was the genome of the bacterium in the 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 - it took me, without exaggeration, about 40 minutes to read the file ( since then I have learned several better ways ). The most important thing is that after reading the file, the rest of the algorithm in the program worked for 10 seconds. Two days later I worked: I made changes in the code, ran the program and managed to watch the whole LOST episode before the program completed its execution. “Damn, on the twelfth line error! Again, everything is new ... "

After a lot of re-launches, I finally thought “there must be a better way to do this.” I found out that you can write a Perl loop that can read the genome in less than one second (at the same time, I could not program with Perl better than Java — I was just lucky). So, I sat down and wrote a Perl script that read the file, put it together in one line and output it. Then I made my Java program invoke the Perl script through the command line, capture the output and save it to a string.

If I had this program preserved, I would have printed its source code and framed it on the wall, in order to remind myself more often that I would never scold anyone for the “bad” code written by him.

I was inspired to share this case from my life with a recent XKCD comic about “code quality”:

image

Comics other than "evil" can not be called - an experienced programmer does not give any useful advice, only holds hyperbolic analogies. But the thing that most caught my eye was the patient's response - “Well, I read the coding style guide.” Will a living person react like that if someone is so rude to him? Wouldn't the answer be like “Well, that was the last time I showed you my code,” or even “Okay, I think I should start with 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 quite understandable), but the first position on their list is usually embarrassed: “my code is too ugly to give it to someone.” Those who seek to shame others for code do not help much in this matter!

The well-known advice to beginning writers says: “Each of us holds within us a million bad lines. Before this million spills out, trying to write something good is useless. So go and write! I remembered it when I read the discussion that flared up about the above XKCD:

When you, an experienced programmer, next time you want to shame someone for his code, try to mentally return to your own million lines of code. Imagine that someone condemned you just as I myself above. Would you continue to ask for help? Would you have mastered your way in a million lines?

John Carmack comment


Do not confuse the words "idiot who wrote this code" and "this code is crap" - there is a significant difference between them. Comics just about the last. I assume that this is done consciously. An attack on the author is unlikely to bring any benefit, but it must be possible to severely criticize the code itself. In real life, many authors fail to 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 shortcomings, 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 as it happened - some of them are excuses, but some of them are pure "WTF did I even think when I wrote this?". And if you do not feel a little guilt for your recent work, then it is quite possible that it will be useful for you if someone points out problems to you - in the sense that this person will be able to break through your "defense mechanisms."

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

>> Do you make a revision code for every commit in Oculus?

Not. We have some progress in this direction; I will be interested to see what happens. We are still strongly affected by the pressure of the pace of work on a startup.

>> Your comment to the article looks like a monkey swept over the keyboard, and then you just walked around for an auto-corrected view ... well, it didn't sound very useful, did it? 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 cases that some minimum of “activation energy” is needed for a comment to have an effect on a person.

>> Did you review code written by other developers on id in the past?

Informally, and then this was enough for me. I do not like doing code revisions; perhaps for this reason I appreciate criticism.

Let John admit that he partly doesn’t keep to strict comments, is far from Linus Torvalds, generously giving his letters personal insults, and his speeches are phrases like “You don’t care, I’m worried about technology and the core - that’s what really important "and the statements that " no one will hear you if you are going to be gentle. "

Source: https://habr.com/ru/post/256887/


All Articles