Categories

Pages

Archives

In search of productivity

Once upon a time, I started to learn programming in C++. It turns out that it’s hard (for some value of “hard”) to do a lot of basic things in C++. As a budding programmer, I spent more time struggling to make myself understood to the compiler than writing code.

When I was introduced to Python, suddenly programming was fun again. It was hard to imagine going back to anything like C++ when I could become so productive so quickly with Python.

I held to this view for a very, very long time. I appreciated how languages like C++ or Java would be useful for writing “serious” applications, but I didn’t write that stuff. That was for other people.

In the last year or so, I’ve gradually come to believe that a strong type-checking system AND a high-productivity language ought not and are not mutually exclusive. The corollary is that I will consider carefully before I use a scripting language for anything more than a thousand or so lines.

The first sign of trouble

The first time I ran into serious trouble with a scripting language was a few years ago (yikes). I wrote a script in Python which would monitor some servers for updated software and kick off a job when they were updated. It was less than 1000 lines, but despite knowing better, I did a poor job keeping it clean. And because it was such a mess, of course I had no tests.

Without unit tests, I had to test it manually. The cycle was something like: write a bunch of code, and run the whole dang script. This was time-consuming and error-prone. I’d miss things. Basic things. The script itself wasn’t mission critical but I found these bugs and the script itself personally embarrassing; I knew better, and I knew I knew better.

Still, I was otherwise quite productive in Python. Around the same time, using the Django framework, I wrote a server which slurped XML-formatted test results and allowed the user to view results grouped by various properties. It was very fast to write, and I could hardly imagine doing it in C++ or Java.

I had three experiences in the last year or two which rearranged my prejudices.

JavaScript and the Closure Compiler

The first was working in JavaScript land, with the Closure Compiler. JavaScript was far less restrictive and, in my opinion, more expressive than Java. The compiler caught some of my dumb mistakes and I grudgingly came to value it. However, it didn’t catch all of my mistakes and the ones that it did not catch were sometimes difficult to track down.

The lesson I learned was that even the slightest type-checking can really help save you from yourself. It also makes refactoring (and thus maintenance) easier; adding or removing a parameter, or passing in the wrong type can be difficult to debug in a language like JavaScript.

Java

The second was working in Java land. I wrote a framework which would test a remote API. I had to do a lot of fighting with the language, with a lot of boilerplate and book-keeping. I missed closures and first class functions dearly; contrary to what I thought at first, Java does have a very limited form of closures, in the form of anonymous inner classes. The lengths I had to go to get certain pieces working was comical; at one point, I wrote a FactoryFactory. I’m not kidding! And I’m not proud of it, but the language and API I was using drove me to it.

However, up to this point in my career, I don’t think I’d spent as much time with a decent IDE on an extended project. Several aspects were revelatory. I’ll mention two.

1) The type system gave me substantially more confidence that I was doing the right thing most of the time. I spent more time fixing incorrect assumptions about the API, adding error-recovery, or simply writing code than chasing down basic mistakes. I do that in any language, but the difference here was that the IDE and language eliminated an entire class of mistakes as I introduced them. By the time I ran my program, I had substantially more confidence about its correctness than I ever did in Python.

2) If you haven’t yet, read Martin Fowler’s Refactoring. The short version is that there are a set of highly mechanical techniques you can use to facilitate refactoring. Furthermore, many Java IDEs support many of these operations. Even something as basic as renaming method calls or variables within a specific context is massively more helpful than dumb S&R.

Lest I seem insane

Despite all that, I still wouldn’t program in Java again by choice. I want closures. I want first class functions. I want built-in higher order functions, for that matter. I want syntax and/or a syntactic paradigm (think variables or methods like fooByBarWithBazAsQuux) that’s substantially less verbose.

That? That up there? It hurts me inside. The compiler makes it harder for you to screw it up, but you still have to do the whole song and dance every. Single. Time. You spend ~6 lines on an operation which, in the grand scheme of your program, ought to be small and trivial.

Python

Speaking of Python, I started relatively recently on refactoring a set of Python scripts responsible for kicking off tests. Nobody owned these scripts and consequently they languished as multiple people had patched or hacked on them to fix bugs or add features. The scripts were a mess.

Furthermore, there are few thousand lines of these scripts, plus thousands of lines more in the form of libraries which the scripts use. There were and are tests, but obviously nobody ran them because they were all broken.

After having spent so much time in the Land of Type-checking, reality came rushing back. It scary to make changes without having any confidence that they were correct. I wrote tests to find basic mistakes which would not manifest until runtime.

I would say that this was the biggest shock and what did the most to change my views. A scripting language is very good for getting something running quickly, but in terms of refactoring and maintenance, it took two or three times as long as I thought it ought to.

A word about tests

You could make a strong case that the scenario above— writing tests to the point of boredom— is Working As Intended. I am strongly sympathetic to this idea.

You ought to write unit tests with good coverage no matter the language. Tools like autonose allow you to get feedback from those tests rather quickly. This workflow arguably gives you faster, more comprehensive feedback than one with just a compiler and manually run unit tests.

One hitch is that getting a good error message from a unit test requires an amount of diligence multiplied by the number of unit tests. A compiler does only catch a small subset of the problems a unit test would, but any decent one ought to catch them more reliably and precisely.

Anyway, I haven’t delved too much into larger-scale projects which are based on scripting languages, but my guess is that their approach is similar to this. Write testable code. Use frameworks which make it easy to write tests (with a nice DSL, flexible mocking, or what have you). Use frameworks which make it easy to run tests (such as speed and parallelism). Minimize the inherent friction and you’ll get something richer than a compiler alone would offer you.

The answer?

I’m still writing tests in Java. I’m still refactoring Python scripts. For obvious reasons it would be silly to introduce a new technology into an existing stack chiefly or even partially for my own personal satisfaction. Rewriting is a dubious proposition anyway, one that must be considered very carefully. So for practical reasons, the situation won’t change.

Even so, I still want to have some go-to language. What could I use for a new project, personal or otherwise which would have good odds of making me happy?

C++ is not memory safe. C is simpler than C++ but is also memory unsafe and has more trade-offs. In both C and C++, it can be very difficult to write basic functionality.

Java is memory safe and has a rich set of libraries, but as I’ve mentioned, I’m not interested.

So what’s the answer? What language or technology do I feel is most promising, offering a combination of strong typing and high productivity? I have one prospect, or even two. But I’ll save those for the next post.