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)

Java for new Programmers

Today was a great session about Java in education

From Oracle

Resources from Ken Fogel’s talk

  • Sample code showing Java brevity
  • Single File Source Code Execution (I really like being able to write java MyProgram.java). Way less explanation for new programmers!
  • Type /edit in JShell to open in an external editor (I didn’t know about this – cool)

Other resources I like

  • Online IDEs
    • https://repl.it/languages/java10
    • https://www.jdoodle.com/online-java-compiler/

Our new Java 11 book comes out next month!

Update (11/05/2020): Read The 1Z0-819 Exam page to learn how you can easily our Java 11 Study Guides to prepare for Oracle’s 1Z0-819 Exam, as well as the 1Z0-817 Upgrade Exam.

Many of our readers have been asking when our next book is coming out for the OCP Java 11 Programmer II 1Z0-816 Exam. Given the worldwide pandemic, it did not launch when we expected it to, but we have good news to share. We spoke with the publisher and it is expected to be released next month! If you are interested, you should probably preorder now, as our last book was sold out for over a month!

But wait, there’s more! Due to the delays we had time to “sneak in” an Appendix for those taking the OCP Java 11 Upgrade 1Z0-817 Exam! So, whether you’re taking the Programmer II or Upgrade Exam, this book has you covered for Java 11.

Full disclosure: Due to the continuing pandemic, it is certainly possible the book could be delayed further. What we have now is the most up-to-date information available from the publisher. If this changes, we will let you know.