Dec 14, 2010

A (not so serious) C# 5.0 Language Suggestion

Occasionally I run across less then optimal code as part of my day-to-day work, as do most people who deal with legacy code bases.  Even if your code is pure as the driven snow, if you’ve ever spent any time looking at the Code Snippet of The Day on the Daily WTF then you’ll have seen this sort of thing before:

           for (int i = 0; i < 200; i++)
{
try { someCollection.RemoveAt(42); }
catch { }
}

Yes, it’s the classic Try, Catch, Swallow pattern in action.  If there’s a problem in there, we put our hands in the air like we just don’t care.

So today one of my fellow Readify colleagues, Ivan Hamilton, suggested a new keyword be added to C# 5.0 to help all those developers out there who are constantly writing this sort of thing.  Behold the OBVLIVIOUS keyword!

           for (int i = 0; i < 200; i++)
{
oblivious { someCollection.RemoveAt(42); }
}

Sheer genius!  That code exhibits the same behaviour and is now so much more readable!

If you were on the C# team, what syntactic sugar would you add to the language?

3 comments:

  1. I've seen 9000 line facade classes that would really be slightly shorter if this was adopted.

    ReplyDelete
  2. Rather than nested tries:

    try { ... } catch { try {...

    I propose `tryharder`:

    try { ... }
    tryharder { ... }
    catch ...

    ReplyDelete
  3. Honestly that would be nice in the sense that when an oblivious keyword was used you could suppress the stack that would normally be generated as part of a try catch?

    ReplyDelete