A weblog by Will Fitzgerald

Dynamic typing and testing

As kind of a 10-finger exercise, I decided to write a small inverted index library in Ruby today, since I had the day off. This is just a few thoughts about what it was like to write tests for this code, which can be found at https://github.com/willf/inverted_index_simple–not that I think you’ll write the next major search engine with this bit of code…

What struck me was that as I was writing unit tests, how many of the tests failed at first either because of undefined variables or dynamic typing problems. For example, in the method query_and (which returns the set intersection of all documents containing query terms), I originally wrote:

def query_and tokens
return Set.new if tokens.size == 0
sets = tokens.map{|token| query_token token}
sets[1..-1].inject(sets[0]){|reduction, x| redution & x}

The third line is only tested if there are at least three query terms, so it was important to write such a test. But what surprised me is that the error was not in the logic, but the literal typing–I misspelled “reduction” the second time.

Similarly, I think I wrote a version of the function that had

sets = tokens.map{|token| query_token tokens}

where I typed ‘tokens’ instead of ‘token’ by mistake.

With a statically typed language, neither of these errors are likely to make it past the static compiler. The Go language, for example, won’t even let you declare variables unless you use them–something which drives me mad, but is probably the right thing to do. (It would have caught the second error, even if ‘token’ and ‘tokens’ happened to be of the same type.)

The great loss of strict static typing comes when it is difficult to write ‘generic’ code (stacks, and queues, for example, of anything); most modern languages provide some sort of facility for this (and we’re still waiting for Go to provide this). The other great loss is when it really makes sense to have union types: functions that work on lists of ints, in addition to ints.


3 responses to “Dynamic typing and testing

  1. brendan o'connor July 7, 2011 at 4:08 am

    Given how much you get from static typing, it’s amazing that dynamically typed languages are ever useful. I think this says more about the state of lightweight tooling for static languages (there isn’t any albeit recent slight progress) than the supposed greatness of dynamic languages.

    I started using the “pyflakes” plugin for vim, which does basic python static analysis — catches typo sort of bugs like undefined variables and such. It makes me *much* more productive at python programming than before — and it’s a shadow of what you get in a real static language of course.

  2. Robert Goldman July 8, 2011 at 7:27 pm

    I was a little confused that you said that static typing was inconsistent with generic code. Languages like Haskell let you do this, and Clojure has its notion of protocols…. Do you mean “mainstream Java and C-style static typing”? Or am I missing something?

    • Will Fitzgerald July 8, 2011 at 7:46 pm

      No, I think I was just being unclear. I didn’t mean to imply they were inconsistent; just that sometimes you don’t get generics, and I was thinking specifically about Go as it is right now. I do hope Go gets them or something like them at some point. There is at least one source-code-to-Go code compiler that implements Go generics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: