Background Unit Testing

2. February, 2009

I just found this article: Background Unit Testing: New Evolutions in Unit Testing and IDE Integration
The idea is that your IDE should run the unit tests in the background just as it runs the compiler in the background. Compelling. There are already two implementations for Eclipse: JUnitMax from Kent Beck and Infinitest.


Testing: Pay in Advance or Afterwards?

2. February, 2009

In a recent post, I talked about people ignoring the cost of some decision. In his blog “Joel on Software”, they talk about the same thing: How easy it is to fall into the “we must have strict rules” trap to protect ourselves against some vague  fear of failure. Only, humans are really bad at sticking to rules. Or are they? Maybe it’s just that reality doesn’t care so much about rules because things change. If you built your castle on the belief how well strong walls will protect you, the swamp around the basement is not going to care. You’re going down, chummer.

So we end up with a lot of rules which make exactly one thing simple: To assign blame. I’ve been working for a big company where we have a strict process how projects were to be set up. There were lots of documents and forms and comittees how to start a project and a lot of documents describing how to end it (put it into production, what documents to file, who to inform, you name it). It was a great process (in the sense of “big”, mind). The actual writing of the code was explained in a document which contained a single page. On that single page, they talked on how they would strive to write excellent, error free code and that they would use a proven strategy, the waterfall model.

They built a huge, shiny castle on nothing.

If you go to a bank and tell them you have lots of $$$ and you need to pay some big bill somewhere in the future, their first question will be: How you want to make that money work for you in the meantime? Just letting it rot under your desk is not very smart, right? You should invest it somewhere, so you will have $$$$$ or even $$$$$$$ when it comes to pay the bill. Which makes sense. Contrary to that, when we write software, we tend to spend our money first instead of parking it in a safe place where it can return some revenue, being ever vigilant to be able to pay as the bills show up. Which is harder than just sitting back and relying on some mythical process someone else has written on a piece of paper a long time ago.

So when you ask: “Should I write tests for all my classes? For every line of code? How should I spend my money?” Then my answer will be: I don’t know. How can I? I know nothing about your project. But I can give you some ideas how to figure it out yourself.

“Should I write tests for all my classes?” That depends on what these classes are meant for. The more low-level the code, the more tests you should have. Rule of thumb: Tests yield more result in the basement. Make sure the ground you’re building on is sound. And behaves as you expect. The upper levels are mostly built from lego bricks. They are easy to take apart and reshape. They are exchangable, so you can get away with fewer tests. But every bug in the foundation will cripple anything above it.

“For every line of code?” No. Never. 1. It’s not possible. 2. Maintaining the tests will cost more than the real code. 3. Tests are more simple than the real code but you still make a constant amount of mistakes per lines of code. So this will only drive the number of bugs through the roof. 4. Strict, fixed rules never work (note the paradox).

“How should I spend my money?” One word: Wisely. Wisely means to think about your specific problem and find the unique solution. Do you know in advance how much each piece will cost? No. So the best you can do is a staggered approach: Invest a bit of money, check how it plays out. If it works well, spend more. If it doesn’t, scratch it, learn, try something else. Which you will be able to do since you didn’t put all your money on a single horse.

So what if your three month venture into agile development didn’t really work out? All you lost is three months. Other projects are deemed a “success” after going over budget by 100%, using twice the time that was estimated (and none of them were shorter than a year). But you will still have learned something. You paid for it, that wisdom is yours.

Use it wisely.


A Different View on Exceptions

30. January, 2009

The discussion about checked/unchecked exceptions is almost as old as Java. While we all have a point in your stance towards this, maybe we are looking at the problem from the wrong angle. Manuel Woelker wrote an article which concentrates on the receiver of the exception, the user, and how exceptions should behave to help the user: Exceptions From a User’s Perspective.

In a nutshell:

[…]the error message displayed to the user should also explain what can be done to correct the situation

Take this code:

Foo foo = cache.get( key );
Preconditions.checkNotNull( foo, "foo is null" );

This error message is wasting someone’s time. Use this instead:

Foo foo = cache.get( key );
Preconditions.checkNotNull( foo, "No Foo for %s", key );

See? That actually tells you why foo is null.

Can we do better than that? Yes, we can:

Foo foo = cache.get( key );
Preconditions.checkNotNull( foo,
    "No Foo for %s. Valid keys: %s",
    key, cache.keySet() );

As you can see, by adding just a little bit of extra information, you can prevent someone (maybe even yourself) from starting the debugger, trying to reproduce the bug and then trying to pry some useful hints of the cause from the runtime.


How to Hide a Virus in Source Code

28. January, 2009

I’ve been looking for quite some time for this article: How can you hide a virus in the source code? Basically, you create a binary of a compiler which contains the virus and which is patched to infect other programs as it compiles them. This is a feature of bootstrapping a compiler.

Reflections on Trusting Trust by Ken Thompson.


One Word: Cute

27. January, 2009

Danc’s Miraculously Flexible Game Prototyping Tiles Enjoy!


Another Lesson on Performance

23. January, 2009

Just another story you can tell someone who fears that “XYZ might be too slow”:

I’m toying with the idea to write a new text editor. I mean, I’ve written my own OS, my own XML parser and I once maintained XDME, an editor written originally by Matthew Dillon. XDME has a couple of bugs and major design flaws that I always wanted to fix but never really got to it. Anyway.

So what is the best data structure for a text editor in 2008? List of lines? Gap-Buffer? Multi-Gap-Buffer?

XDME would keep the text in a list of lines and each line would point to a character array with the actual data. When editing, the characters would be copied into an edit buffer, the changes made and after the edit, the changed characters would be copied back, allocation a new character array if necessary.

This worked, it was a simple design but it had a flaw: it didn’t scale. The length of a line was limited to the max size of the edit buffer and loading a huge file took ages because each line was read, analyzed, memory was allocated … you get the idea.

So I wanted to make it better. Much better. I’d start with reading the file into a big buffer, chopped into evenly sized chunks to make reading both fast and memory efficient (imagine loading a 46MB file into a single memory chunk – after a couple of changes, I’d need to allocate a second 46MB chunk, copy the whole stuff over, etc, needing twice the amount of RAM for a short time).

During the weekend, I mulled the various ideas over, planned, started with a complex red-black tree structure for markers (positions in the text that move when you insert before them). It’s huge, complex. It screams “wrong way!”

So today, I sat back and did what I should have done first: Get some figures. How much does it really cost to copy 4MB of RAM? Make a guess. Here is the code to check:

    public static void main (String[] args)
    {
        long start = System.currentTimeMillis ();
        
        int N = 10000;
        for (int i=0; i<N; i++)
        {
            int[] buffer = new int[1024*1024];
            System.arraycopy (buffer, 0, buffer, 1, buffer.length-1);
        }
        
        long duration = System.currentTimeMillis () - start;
        System.out.println (duration);
        System.out.println (duration / N);
    }

On my machine, this prints “135223” and “13”. That’s thirteen milliseconds to copy 4MB of RAM. Okay. It’s obviously not worth to spend a second to think about the cost of moving data around in a big block of bytes.

That leaves the memory issue. I would really like to be able to load and edit a 40MB file in a VM which has 64MB heap. Also, I would like to be effective loading a file with 40MB worth of line-feeds as well as a file which contains just a single line with 40MB data in it.

But this simple test has solved one problem for me: I can keep the lines in an ArrayList for fast access and need not worry too much about performance. The actual character data needs to go into a chunked memory structure, though.

Morale: There is no way to tell the performance of a piece of code by looking at it.


25 Most Dangerous Programming Errors

23. January, 2009

If you want to improve your l33t [0d|ng skillz, especially keeping script kiddies off your back, here is a list of the 25 most common coding errors: http://www.sans.org/top25errors/


Sorting for Humans: Natural Sort Order

22. January, 2009

Kate Rhodes sums it up nicely:

Silly me, I just figured that alphabetical sorting was such a common need (judging by the number of people asking how to do it I’m not wrong either) that I wouldn’t have to write the damn thing. But I didn’t count on the stupid factor. Jesus Christ people. You’re programmers. You’re almost all college graduates and none of you know what the f**k “Alphabetical” means. You should all be ashamed. If any of you are using your language’s default sort algorithm, which is almost guaranteed to be ASCIIbetical (for good reason) to get alphabetical sorting you proceed to the nearest mirror and slap yourself repeatedly before returning to your desks and fixing your unit tests that didn’t catch this problem.

So if you want to sort your lists the right way (instead of the ASCII way), read this.


The Temporal Void

3. January, 2009
Cover of "The Temporal Void"

Cover of The Temporal Void

Holidays. The only time where I can read or “dream with open eyes” (text from a bookmark). This year, it was “The Temporal Void” by Peter F. Hamilton. It’s the sequel to “The Dreaming Void” (my review).

Again, the series is coming along great (which Peter can probably see on your bank account 🙂 Well deserved if you ask me). I like the rich characters, the story is sound and believable. Recommendation: Buy. Now.

There were three spots which I didn’t “buy” in “The Temporal Void”, places where I dropped from the story and thought “WTF?” Note: Only mild spoilers below; you can read on even if you haven’t read the book, yet.

  1. So Aaron is stranded on Hanko, the planet is about to blow up and the Navy scout is about to pick him up. After being warned that he’s dangerous, having the best sensors military money can buy, they let him simply walk on their ship battle ready and kill them. I mean, OK, shit happens and maybe these was the Omega ship with the best morons the Navy could find and such … but … nah, really 🙂 With instant comm available at all times, no one is watching this important operation? There isn’t even a recording? Didn’t buy that one.The same happened in the first part when Aaron broke into the storage vault to claim Inigos memories. Why did you place the guards *inside* (where all that delicate stuff will break if they ever would have to engage someone)? Why not place them on the other side of the vault door where they can pummel any intruder against a foot or two of solid steel, without any cover?
  2. Edeard finds his childhood friend Salrana in the clutches of Ranalee and leaves her there. I never thought he would be the character to leave someone behind. He knows only bad can come from this; I mean it’s only the tenth time this happens, he got to learn something, right? If he dragged Salrana away, the girl would be mad but he could leave her with the Pythia and look for a solution if she doesn’t know one. If all else fails, he could simply blackmail Ranalee into fixing what she did. So I accept that he’s tired and worn out and all that but this just didn’t fit.
  3. Paula and the quantumbuster. So this thing really distorts spacetime to wreak havoc with matter. How can she get away when space is so twisted? How about just nailing her in place using the ships in orbit and blowing up the station the traditional way?

Other than that, the story is the usual perfect piece of work from Peter. I’ve posted the text above in Peter’s inbox; should I get a reply, I’ll post it here.


Hit Them Harder Until They Learn

23. December, 2008

Do you believe that hitting people harder will make them learn? No? Do you believe that prison will turn a bad person into a good citizen? If not, why don’t you do something about it?

People like simple solutions. Out of the eyes, problem solved. Lock someone up, it will turn them into a nice fellow. Because you don’t like to be locked up, you believe that others feel the same. The question remains: Why should locking up someone turn them into a better person?

Well, it does work … for monks. They willingly lock themselves up somewhere to be able to concentrate on those things that are dear to them and where they want to improve. But there is a big difference between locking up yourself (like a hermit) and locking up someone with a bunch of criminals and guards. Just make a wild guess where most criminals learn their trade …

The relapse rates vary a lot (by age, type of crime, prison, treatment). Still, I’m more astonished by the fact that not all of the inmates become criminal again rather than the fact that some do.

That said, it is refreshing when you meet people who don’t fall for the temptation of retribution. The guys at stackoverflow.com recently discovered that some people gamed the system to hurt other members or to increase their status. Instead of punishing them, they decided to just take away any advantage of trying to game the system. Problem really solved. Carry on, commander!

Links: Vote Fraud And You
Ein Gefängnisersatz mit Rückfallquote null (German)
GEFÄNGNIS – LEBEN HINTER GITTERN (German)