3.8. Jon Skeet

For those developers who really want to delve into the details of a language, I don't think you can do much better than reading Jon Skeet's C# in Depth (Manning, 2008). A revised version for .NET 4.0 is currently on its way (see http://csharpindepth.com).

I spoke to Jon about his thoughts on C# 2010.

What do you see as the top feature(s) in C# 2010, and why?

Named arguments and optional parameters, without a doubt. (That sounds like two features, but they naturally come together.) It's a small feature, but it's likely to be the most widely used one. Two of the others (better COM support and dynamic typing) are only likely to be used by a minority of developers, and while generic variance is useful and interesting, it's more of a matter of removing a previous inconvenience than really introducing something new.

Admittedly, to fully take advantage of optional parameters, you have to be confident that all your callers will be using a language supporting them. For example, suppose you wanted to write a method with five parameters, three of them optional. Previously, you may have used several overloads to avoid forcing callers to specify arguments for parameters where they're happy with the default. Now, if you don't care about (say) C#2008 callers, you can just provide a single method. But that would force any C# 2008 callers to specify all the arguments explicitly.

The biggest potential use I see for the feature is immutability. C# 2008 made it easy to create instances of mutable types using object initializers, but provided no extra support for immutable types. Now with named arguments and optional parameters, it's a lot easier. For example, take the initialization of a mutable type:

Person p = new Person {
    Name = "Jon",
    Occupation = "Software Engineer",
    Location = "UK"
};

This can be converted into initialization of an immutable type without losing the benefits of optional data and explicitly specifying which value means what:

Person p = new Person (
    name: "Jon",
    occupation: "Software Engineer",
    location: "UK"
);

Are there any new features in C# 2010 that you would avoid using or that have the potential to encourage bad programming?

Well, dynamic typing is going to be really useful when you need it, but should generally be avoided otherwise, in my view. It's great for interoperability with dynamic languages via the DLR, some additional COM support, and occasional cases where you would otherwise use reflection. But C# is basically a statically typed language. It isn't designed for dynamic typing. If you want to use dynamic typing widely throughout a program, write it in a dynamic language to start with. You can always use the DLR to work with C# code as well, of course.

What would you like to see in the next version of C#?

More support for immutability. For example, read-only automatic properties would be a simple but really helpful feature:

public string Name { get; readonly set; }

That would be backed by a read-only field behind the scenes, and the property could only be set in the constructor (where the assignment would be converted into a simple field assignment). Beyond that, the ability to declare that a type is meant to be immutable, with additional compiler support and checking, would be great. But that's a bigger request.

If Code Contracts takes off, it would also be nice to embed the simplest contract, nonnullity, into the language, in the way that Spec# did. For example, instead of

public string Convert(string input)
{
    Contract.Requires(input != null);
    Contract.Ensures(Contract.Result<string>() != null);

    // Do processing here
}

you could just write

public string! Convert(string! input)
{
    // Do processing here
}

The handling of nonnullable local variables could be tricky, but there are smart people at Microsoft. I'm sure they could figure something out.

Admittedly, I'm wary of anything that links the language too closely to specific libraries, but this would be a really nice win in terms of readability.

These may all sound like I'm lacking in ambition. After all, there are bigger ideas on the table, such as metaprogramming. But I'm really keen on small, simple changes that make a big difference. I generally need to be persuaded more when it comes to large changes. The ones we've already had in C# have been very well designed, and I'm pleased with them. But the language is getting really pretty big now, and I think we need to make sure it doesn't become simply too hard to learn from scratch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.147