New C# features – my criteria for evaluation

I've been gaining experience with some of the new C# 2.0 features and am planning to post on a few of them. Before jumping in, I wanted to give my criteria for evaluating them.

Readability - Readability == maintainability, and this is one of the most important factors to me. Writing maintainable code is incredibly important to me because you spend way more time reading code (your own or someone else's) than writing code.

Performance - Performance is definitely something I care a lot about. However, in most cases I prefer readability to performance, except in code that's proven to be a performance bottleneck. In many cases, it's a judgment call. I've run into far more cases of bugs caused by confusing code than I've run into performance problems caused by using one language construct over another.

Writability - Many new features let you express something more concisely than in the past, saving the programmer lots of typing. This is not something I care about at all. You spend far less time writing code than reading it. If you save some time typing but end up with less readable code, that's a horrible trade off. That's why we use descriptive variable names, etc.

As an example, take a look at the following code:

string a;


// ...


string b;

if (a != null)


    b = a;




    b = "foo";


This is quite straightforward, but a bit verbose. The ? operator makes this much more compact:

b = (a != null) ? a : "foo";

When you first learn a C-based language, this can seem a bit unnatural, but you quickly learn the idiom and it ends up improving readability. It's especially nice when using it as a parameter or somewhere that prevents the need for a temporary variable. It has lots of potential for misuse though. When you use complex expressions with the ? operator or do something nasty like nest them, you can end up with something that's concise, but completely unreadable.

C# 2.0 adds a bunch of syntax sugar for Nullable<T>, including a ?? operator that lets you write:

b = a ?? "foo";

My first reaction to seeing this was yuck! But, I think that's just because the syntax was new to me. On further reflection, I think it's pretty nice. Once you get used to this idiom, it expresses what's going on very clearly and has much less potential for misuse. At least, that's what I think will happen when I get used to it 🙂

Comments (7)

  1. Sriram says:

    God! Please dont tell me people are going to start use ternary operators again. The horror! Can you imagine the amount of bad code that would be churned out?

  2. Kevin says:

    Hey Dan, what does ?? actually *do*? 🙂

  3. Dan Crevier says:

    Sorry I wasn’t clear. a ?? foo does the exact same thing as (a != null) ? a : foo. It lets you specify a value to be used for a nullable if it’s null. All reference types are nullable, so it can be used with them. It can also be used with Nullable<T>’s.

  4. Jeff says:

    so we now get the perl idiom:

    my $foo = $bar | "bar";

  5. Dan Crevier says:

    My perl is a bit rusty, but wouldn’t $bar | "bar" be more like:

    foo = String.IsNullOrEmpty(bar) ? "bar" : bar;

Skip to main content