The Importance of Knowing Your Language – Micro Edition

I was reading about “fast” vs “slow” programmers and came across the assertion that there’s no such thing as fast or slow programmers given the same coding activity. And that being able to find an example of the same/similar problem. And solving the right problem.

I agree that being able to find an example is important. But knowing the language/API well saves a lot of time. As an experiment, I solved the same problem in Java (which I know fluently and use almost every day) and Python (which I know enough to cobble stuff together and don’t use often). As a “handicap”, I used vi for both so the IDE wouldn’t make me faster in Java.

The problem

Read a file and remove all the lines that have “slow” in any case.

Java

This took me just under 4 minutes. About half of which was spent looking up the imports and dealing with stupid stuff the IDE would have handled (throwing the exception and mispelling “output”

The solution:

import java.nio.file.*;
import java.util.*;
import java.io.*;

public class Process {
  public static void main(String... args) throws IOException {
    Path input = Paths.get("input.txt");
    Path output = Paths.get("output.txt");
    List<String> lines = Files.readAllLines(input);
    lines.removeIf(s -> s.toLowerCase().equals("slow"));
    Files.write(output, lines);
  }
}

Python

The same task in Python took just over 7 minutes.

The solution:

input = open("input.txt", "r")
output = open("output.txt", "w")
for line in input:
  if  line.lower().strip() != 'slow':
    output.write(line)

input.close()
output.close()

Conclusion

I didn’t become a slower programmer between writing the Java and Python examples. But I was slower. Because I had to look up more in order to accomplish the task. Someone who programs in Python more often could have whipped it out faster.

I’ve interviewed people who couldn’t have done it in either language in under 10 minutes.(I don’t ask this question at interviews but I have questions of comparable difficulty).

Plus people think and type at different speeds even if they know everything without looking anything up.

And a Funny Story

I was pair programming with a junior developer recently. When he was typing, we needed to read a file and do something with the contents. As evidenced by the above, I’m very familiar with the idiom for reading a file. When he said, “I’ll google reading a file”, I asked to take a turn at the (virtual) keyboard. As I typed the Path/readAllLines() idiom, I commented, “I”ll be google”. I have full confidence my teammate could have Googled this. And that he would have gotten correct code. But it was faster for me to type it. And he still got to observe the idiom either way. (Plus my way didn’t risk him finding the pre-Java 1.7 way and learning that)

Leave a Reply

Your email address will not be published. Required fields are marked *