October 25th, 2005

Illuminati

Dreams

I had a dream last night where Mal (from Firefly) was kicking the ass of Sawyer (from Lost).

No, I have no idea why.
Illuminati

switch statements in C#

Don't read any further unless you either know or care about (a) C# and (b) syntax

And no, this isn't a complaint about break - I'm very happy to make things explicit.
However, why is the format
switch (myInt)
{
    case 1:
        //Do Something
        break;
    case 2:
        //Do Something Else
      break;
    default:
        //Do nothing at all
        break;
}

rather than something like:
switch(myInt)
{
    case 1:
    {
        //Do Something
    }
    case 2:
    {
        //Do Something Else
    }
    default
        // Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and it follows the same way of grouping commands as the rest of the language does.

_Ideally_ it would go even further, to something even more like if statements:

switch(myInt)
{
    case (1)
    {
        //Do Something
    }
    case (2)
    {
        //Do Something Else
    }
    default
        // Do Nothing at all
}

and you wouldn't be able to use goto to jump from one case to another (you'd have to take any shared code and put it in a separate method).

This could then be extended into a more generalised form (less optimisable, but still very readable) for those occasions when you don't want to necessarily check a single variable, but instead want to check a variety of cases, each one of which is exclusive - like so:

switch()
{
    case (Country == "US")
    {
        //Deal with Americans
    }
    case (Age < 18)
    {
        //Deal with foreign children
    }
    case (Age < 30)
    {
        //Deal with foreign young adults
    }
    default
    {
        //Deal with everyone else
    }
}

This is much more readable than lots of if...else if...else if....else statements.  And the structure doesn't break any existing code, so it would be possible to add without causing rewrites.  It's also more like the rest of C#, in that it uses curly braces to hold blocks of code.
Illuminati

Note to spammers

Hitting me with identical spam _7 times_ in the space of a minute, with slightly different 'from' addresses makes me _less_ likely to open it, not more.