Author Archives: david

Word definitions

Overengineer (verb): The process of attempting to come up with a solution which suffers from an excessively high level of not being half-assed.

This entry was posted in programming and tagged on by .

Testing, testing

Not the blog, the code.

Among the many weird and wonderful iterators I have in my personal JGreenspun toolkit is one called the FlatteningIterator. It’s a useful little widget for recursively digging through mixed collections of objects and collections. Completely impossible to give a decent type signature to unfortunately, but what can you do? (I mean, I can conceive of a type system in which you can, but it’s basically going to have to be dependent typing). It forms a major chunk of the functionality for my recent WebCrawler implementation.

Indeed, that implementation is exactly what I want to talk about – the very first time I came to run the webcrawler it failed horribly. Rather than, as one might expect, being a failure in fetching or parsing the web page, the guilty party was my nice little utility iterator.

In my efforts to preserve laziness (that is, an object would never be fetched until it was needed), I’d basically screwed up. But I’d done it in a mildly subtle way so I never even thought to unit test it. The iterator’s hasNext() method returned true iff there was a non-empty iterator available to it.

Can you spot the problem? If the iterator available is an iterator which only returns empty iterators then the flattened iterator will falsely report that it has a next element, so using the class while(iter.hasNext() { iter.next(); } loop will throw an unfriendly exception. Unfortunately I completely failed to spot this until it came up in actual practice.

Thus, even though I had extensive unit tests for this class, they missed the one way in which it was actually broken. (The class passed all the other unit tests first time). This is because logic errors in the implementation often correspond to failures to spot an edge case. So if the same person doing the implementation is writing the unit tests, the unit tests probably won’t cover that edge case either.

Fortunately it only took a little time to figure this out, and once I had the unit test was updated to cover this case. So, no real harm done. But I’d have been really annoyed if it had taken me a long time to track down a bug and it turned out to be in code I thought was safe due to unit testing.

This entry was posted in programming and tagged on by .

Code Snippets

I’ve discovered and started using an amusing new site recently. Code Snippets. It basically lets you share snippets of code you’ve written (as the name might suggest). So far I’ve only got one snippet, but I expect there will be more soon – I often write amusing utility classes, and this looks like a good place to share them.

This entry was posted in programming and tagged on by .

Java Packages

Hm. Thought for the day – the apparent hierarchical structure of Java’s package system is in fact a blatant and malicious lie and has no connection to a program’s semantics – classes in package foo.bar have no relation to classes in package foo.

It’s very annoying when you forget this and things don’t work because of it, as it matters sufficiently rarely that you don’t actually think about it and it takes a long time to troubleshoot.

Sigh.

This entry was posted in programming and tagged , on by .

Coding standards

I’ve grown to really hate the coding standards at work, in a few key regards. Not coding standards in general, but ours just seem designed to make more work for us.

Most of them really are more work, but some of them have a useful purpose even so (some of them don’t. I’m really sick of preceding field names with m). I’ve been writing a bunch of code this weekend for a toy project of mine; previously I’d more or less stuck to our coding standards with a few minor omissions. In particular I was using the same layout at work. But, because of the way I’m increasingly coding, this was resulting in obscene amounts of whitespace. So, I decided to change it a bit and see where it took me. One thing led to another, and with the new layout I was increasingly inclined to follow my new approach.

Without further ado, here’s a snippet of some sample code.

So, in summary, why do we have these coding standards? It’s to protect you from people like me. :-)

This entry was posted in programming and tagged on by .