Dutton's blog

New Year, New Host

A year and a day after my last post, here I am again. I’ve finally got around to moving from WordPress on Azure, which despite being total overkill for my use, is hideously slow on their ClearDB MySQL instance to the point of being unusable to Jekyll hosted on GitHub Pages.

Do I use a 'switch' or sequential 'if's? A shallow dive into their CIL

I recently found myself needing to convert a calculation on ints to use double values. One of the functions within this calculation was a switch statement and as switch only supports integral types, this needed more than just a type change.

I was fortunate in that this particular double value was always at most one decimal place which left me with two possible options; either replace the switch with a bunch of sequential if statements or multiply the double by 10, cast it to an int and switch on the resulting int.

Ignoring the debate over whether the cast is a bit too hacky, it got me thinking; what is the difference in compiled output (CIL) between switch and sequential ifs covering the same test cases? Given that the cases of a switch are compile time constants and together as a block, does the compiler make any attempt to optimise the comparison or does it just grind through all the cases linearly until a match is found (making it essentially syntactic sugar for sequential ifs).

Out came a test project and ILSpy and this is what I found.

Here's the test code and corresponding annotated CIL (Release build, .Net 4.5):

public static int SwitchTest(int x) 
    switch (x) 
        case 10: return 1; 
        case 20: return 2; 
        case 30: return 3; 
    return -1; 

UPDATE if exists, INSERT if not in MySQL (AKA the UPSERT)

It's a bit of a long running joke that I'm not really a database guy, but let's just say that lately I've been exposed to a little more DB than usual and it's been quite a steep learning curve.

Anyway, this little feature in MySQL has proved pretty useful to me so I thought I'd make a note of it on my in-lieu-of-a-functioning-memory blog.

As the title says, what if you want to  combine the Create and Update from your CRUD code in one query; say you're performing an INSERT with a nested SELECT to insert more than one row, but there's a chance that one of these inserts breaks a unique key constraint somewhere and so you want to UPDATE in that case instead?

This is where INSERT ... ON DUPLICATE comes to the rescue.

How to create an array with a lower bound other than zero in .Net

Did you know that you can create an Array with a lower bound other than zero in .Net?

A good friend of mine (cheers Jarek) showed me this a while back. I'm still yet to find a real-world application but it's interesting all the same.

Array has a method called CreateInstance, which is useful if you want to create an Array during run-time of a Type which might not be known beforehand. This will create an array of integers with length 5:

var array = Array.CreateInstance(typeof(int), 5);

One overload also lets you specify the lower bounds of the array. It is used to create multi-dimensional arrays, so you pass in arrays of lengths and bounds. This will create an array of integers, with length 50, indexed from 10.

var array = Array.CreateInstance(typeof(int), new int[] {50}, new int[] {10});

I suppose this may come in useful when trying to model an existing offset numbering scheme in your domain, but has anyone come across this or used it before?

Silly for loop syntax

I've been working with C-style languages for over 12 years now, but stumbled across this funny for loop syntax in a code tutorial today and had to double-take:

for (int i = 2; i --> 0;)
    // do something

Although technically correct, quite what you gain from putting the iterator within the condition apart from confusion is lost on me.