You don’t get the C# 4.0, do you?

Sorry for the daring topic, but that’s really what comes to my mind when I read or hear people whining about C# loosing purity/object-orientation/strongly-typedness/goal/insert your favorite here, with the advent of version 4.0.

To sum it up, there are four major new areas of improvement coming up with the forthcoming version of the language, as outlined by Charlie Calvert in “New Features in C# 4.0”.

  • dynamic keyword (type) along with all its implications.
  • Optional and named parameters.
  • Co/Contra-variance of generics
  • Few additional features geared towards easing the pain of dealing with COM

I don’t really care about COM, and covariance and contravariance is not really controversial (at most people will grumble that they have to learn how to use it properly), so I’m going to leave those two out, and concentrate on the first two features.

Optional and named parameters wouldn’t probably even make it into the language if it wasn’t for improving interoperability with COM where they are heavily used. And I’m glad they did. Really. Major argument people raise when it comes to them, is that they add no value to the language. Anything you can do with them, you can do with method overloads. Yes you do, but step forward if you can tell me straight into my face, that you’d rather write this:

public virtual void AddComponent(String key, Type classType)
{
    AddComponent(key, classType, classType);
}
 
public void AddComponent(string key, Type classType, LifestyleType lifestyle)
{
    AddComponent(key, classType, classType, lifestyle);
}
 
public void AddComponent(string key, Type classType, LifestyleType lifestyle, bool overwriteLifestyle)
{
    AddComponent(key, classType, classType, lifestyle, overwriteLifestyle);
}
 
public virtual void AddComponent(String key, Type serviceType, Type classType)
{
    AddComponent(key, serviceType, classType, LifestyleType.Singleton);
}
 
public void AddComponent(string key, Type serviceType, Type classType, LifestyleType lifestyle)
{
    AddComponent(key, serviceType, classType, lifestyle, false);
}
 
public void AddComponent(string key, Type serviceType, Type classType, LifestyleType lifestyle,
                         bool overwriteLifestyle)
{
    /*actual code*/
}

than this:

public void AddComponent(string key, Type serviceType,
    Type classType: serviceType,
    LifestyleType lifestyle: LifestyleType.Singleton,
    bool overwriteLifestyle: false)
{
    /*actual code*/
}

It means less code to write, less code  to maintain, less code that you’d do probably copy/paste, which attracts bugs like a dead dog attracts flies. And while we’re talking about bugs, think about methods like this:

public XPathNodeInfoAtom(string localName, string namespaceUri, string prefix, string baseUri,
                         XPathNode[] pageParent, XPathNode[] pageSibling, XPathNode[] pageSimilar,
                         XPathDocument doc, int lineNumBase, int linePosBase)
 
 

What is really the chance that you’ll get all the parameters right each time you call them, not mixing the order, and passing localName instead of prefix, or the other way around? Think about how utterly hard to read the code is, especially if you don’t have well named variables you pass as parameters. This can be fixed by explicitly passing each parameter along with its name. And it’s not the case only for methods that take that many parameters. One common mistake I see is in constructors of ArgumentException and ArgumentOutOfRange exception, both of which take parameterName and message as a value, but in different order.

The feature that gets the most bad publicity is the dynamic programming capabilities introduces thanks to dynamic keyword. That’s what makes people lament, that they will no longer be able to rely on IntelliSense and the compiler, and that this is the advent of apocalypse.

Well, rest assured – world will stand tomorrow, just like it stood yesterday. The whole dynamic thing gives you three things:

  • Simplified programming model for things you had to rely on strings (ergo, no compiler checking anyway) in the past, so no harm done here.
  • Improved performance as compared to what you could achieve with reflection thanks to smarter runtime resolution.
  • Less ugly code in those places, thanks to not having to use reflection or any other crazy mechanism to circumvent inability of making dynamic calls.

I hear that you say that this may be abused in the multitude of ways. Well – yes, no compiler or language let’s you stop thinking (and if there were such compilers or languages, we’d be out of job!). That is the case with everything though. C# has support for pointers, you know? It was added to deal with certain narrow range of scenarios, and it was there since v1.0 but I don’t see it used all over the place by anyone. Heck, there are features in the language many experienced developers didn’t even heard about, left alone used them. Example? – stackalloc keyword. Did you know about it? Have you seen actually code that uses it? I haven’t.

You can cut yourself with just about anything. We want more power in the language, but with power comes responsibility. The dynamic keyword hugely helps in certain scenarios, and if used wisely can do you nothing but good.

To wrap it up, I think the whole situation is best described by this quote from Andy Baio:

I’m sure that the moment man discovered fire, there was some guy nearby saying, “Too smoky. Can burn you. Lame.

Technorati Tags: ,