Schoolboy Error Of The Day

4

This dumb mistake just cost me an hour spelunking around in the debugger:

var status = source.Substring(source.LastIndexOf("/" + 1));

(where source is e.g. “http://foo.com/status/all-is-good”)

Fortunately the ramifications were picked up in the acceptance tests, but the root cause wasn’t at all obvious from such a high level.

Lesson for the day – code is never too trivial to warrant unit testing.

  1. John Topley
    John TopleyWednesday, 17 August, 2011

    Ha ha, I see what you did there, Ian! I must admit that I always struggle with getting substring operations correct off the top of my head – pesky off by one errors!

    And blimey, C# really is like Java, but with different capitalisation, isn’t it? Is the status variable in your snippet a string, or is it like the old VB Variant?

  2. ianfnelson
    ianfnelsonWednesday, 17 August, 2011

    It’s a String. The Substring method returns String, so the compiler can implicitly determine the type. This was a C#3.0 innovation:

    http://msdn.microsoft.com/en-us/library/bb383973.aspx

  3. John Topley
    John TopleyWednesday, 17 August, 2011

    Cheers. I wish Java had that. Instead of the redundant: Foo foo = new Foo();

  4. Adam Sharif
    Adam SharifWednesday, 17 August, 2011

    I finally see it – spent a few minutes trying to see what was wrong, did the typical thing and went way overboard before noticing a simple mistake :)

Leave a Reply