Nerd explained

I’ve always kind of thought of myself as a geek, or a nerd, or something in that genre. But I have never really understood the subtle differences between them. Recently I came over a good Venn diagram that explains it all! So from now on, this is the definition I will go by ๐Ÿ˜›

Nerd venn diagram

Yes, that diagram is also what I based my site title and tagline upon ๐Ÿ˜‰

New blog!

Alright, we are doing this again. After having watched a very inspiring session with Scott Hanselman called Making Your Blog Suck Less: Social Networking and Your Personal Brand Online at the Norwegian Developer Conference 2009, I figured I should have a go at this thing again. I have had various websites before and a couple of blogs, however they all suffered from me wanting to program more than I wanted to write. That means I usually ended up programming my own stuff, having loads of fun designing, putting things together, forcing some text about myself and what I do out there, and then… well, then they pretty much died.

So, now I am going for WordPress and is hoping that I will manage to stick with it for a while. And hopefully get some stuff out here too. Will try out Scott’s advice on using drafts in Live Writer, etc.

Aaaaanyways, have a look around, drop me an email if you want, comment on stuff, subscribe to my feed, et cetera. Or just ignore me. That is fine by me too ๐Ÿ˜›

By the way if you are interested in seeing that very good session, you can find them here. I recommend downloading it using the BitTorrent file you find there. If you think it is a bit much, just use a decent BitTorrent client like ยตTorrent and you can select just that file. However I really do recommend that you download the whole thing cause it is a lot of really great stuff there. For example some very good introduction sections to Test-Driven Development with Roy Osherove.

C#: Handy BCrypt class for hashing passwords

I was working on an application where I needed to store user names and passwords in a database, as we often do. As we all (should) know we never (ever, ever) store passwords in plain text. If we do, we are setting ourselves up for big trouble if the database contents leaks out or someone hacks their way into it. So what should you do?

You should salt the passwords and you should hash them, and hash them good.

Using raw hash functions to authenticate passwords is as naive as using unsalted hash functions. Donโ€™t. – Thomas Ptacek

So, I was looking for a good implementation of a good hashing algorithm and found one written by Derek Slager called I really like it. It has a very clean interface and is super easy to use. So to make sure I don’t lose it (if he would remove it or I would lose the link or something), I post it here. And if it helps someone else to discover it and to ease their day a little, that would be awesome too ๐Ÿ™‚

You use it like this:

// Pass a logRounds parameter to GenerateSalt to explicitly specify the
// amount of resources required to check the password. The work factor
// increases exponentially, so each increment is twice as much work. If
// omitted, a default of 10 is used.
string hashed = BCrypt.HashPassword(password, BCrypt.GenerateSalt(12));

// Check the password.
bool matches = BCrypt.CheckPassword(candidate, hashed);

You find the class here.

C#: Natural sorting

When you create an application that displays data in lists or tables, you often run into the problem of sorting. When dealing with only numbers it isn’t a big deal, but when sorting text it can be. Regular sorting is often done by alphanumerically, which means that ‘bear’ comes before ‘cat’ and ‘5’ comes before ‘7’. The problem is that this is done letter by letter, which works for most of the time, except when you get numbers in with your text. Then you end up with for example ‘2’ coming after ’10’, since ’10’ starts with a ‘1’ which comes before ‘2’. The solution to this is something called natural sorting.

I won’t write a lot about that here, but just say that it tries to sort things the way humans do. Anyways, below you find a C# class that handles this for you. I put it together by looking around and taking some bits and pieces from here and there, so I can’t really take credit for it. I only post it here so that I won’t lose it, cause it was really helpful.

The class uses some built-in sorting functions in windows and implements the IComparer interface. It can for example be used with the OrderBy extension methods and the Sort methods of List.

using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;

namespace Geekality
    public sealed class NaturalStringComparer : IComparer<string>
        private readonly int modifier = 1;

        public NaturalStringComparer(bool descending)
            if (descending)
                modifier = -1;

        public NaturalStringComparer()
            :this(false) {}

        public int Compare(string a, string b)
            return SafeNativeMethods.StrCmpLogicalW(a ?? "", b ?? "") * modifier;

    public sealed class NaturalFileInfoComparer : IComparer<FileInfo>
        public int Compare(FileInfo a, FileInfo b)
            return SafeNativeMethods.StrCmpLogicalW(a.Name ?? "", b.Name ?? "");

    internal static class SafeNativeMethods
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        public static extern int StrCmpLogicalW(string psz1, string psz2);

When to check for null

I often get truly tired of all the null checking when programming. I keep asking myself, is this really necessary? For example, I am the one coding it, and I am the only one who will be sending stuff into those methods. This is of course not always the case, but very often it is.

Anyways, I found some good advice on when we should check for null values and thought I could note it down here and share it.

One thing to remember is that the code you write today, while it may be a small team and you can have good documentation, will turn into legacy code that someone else will have to maintain. I use the following rules:

  • If I’m writing a public API that will be exposed to others, then I will do null checks on all reference parameters.
  • If I’m writing an internal component to my application, I write null checks when I need to do something special when a null exists, or when I want to make it very clear. Otherwise I don’t mind getting the null reference exception since that is also fairly clear what is going on.
  • When working with return data from other peoples frameworks, I only check for null when it is possible and valid to have a null returned. If their contract says it doesn’t return nulls, I won’t do the check.

Very good advice ๐Ÿ™‚

MySQL: How to reset the root account

I just managed to mess up the MySQL root account. Not a smart idea. After some MySQL manual reading and some serious Google-Fu, I figured out how to fix it.

  1. Stop the mysql server
  2. Add the following to the my.cnf file
  3. Start the mysql server
  4. Start a mysql client and run the following query
    REPLACE INTO mysql.user VALUES ('localhost','root',PASSWORD('blah'),'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);
  5. Quit the mysql client
  6. Stop the mysql server
  7. Remove what we added to my.cnf in step 2
  8. Restart the mysql server


With a hint of Social Ineptitude