Geeks With Blogs
// ThomasWeller C#/.NET software development, software integrity, life as a freelancer, and all the rest

Usually, I'm not what you would call an early bird. That's why I have read about the new dynamic keyword in C# 4.0 only now. To say it clear: I was horrified. My first intuition was to write an FxCop rule to blame all its usages in the analyzed code. Instead, I wrote this post, which is basically a rant against dynamic - simply because writing a post is much quicker and I felt the urgent need to express my opinion on this, but I'm still thinking about the FxCop thing...

The basic fact is in short, that C# is getting VB-style Variants. This is extremely bad news for someone who once quit his job partly because of a totally rotten VB6 codebase, that was cluttered with Variant-declares. But my concerns are not only personally motivated: The use of Variant all over the place created massive maintenance nightmares, thus occupying most of the companies development resources,  and costing tons of money. And the same will happen with C#'s dynamic to the entire IT industry to a certain extent. Its usage might be a time-saver in the short, but it certainly decreases code quality and thus will most likely produce serious maintenance problems in the long run - which is certainly not the right direction to go for an industry that has not quite the best reputation when it comes to things like error-freeness and reliability.

The dynamic keyword opens the door to unsafe typing and gives way to a more careless, lazy kind of coding. Why should I think about what the appropriate variable type may be, when I have the "one-size-fits-it-all" dynamic at hand? In consequence, dynamic makes it way too easy to introduce errors, and it promotes a cultural shift in C# programming style that makes me shudder.

Now I hear the advocates of dynamic say: "When used wisely by well-trained developers, it is a very powerful feature." That's of course true, but it is like saying: "If you always drive your car carefully, you won't ever need a seat belt." Frankly: Would you even consider buying a car that has no seat belts? Certainly not, and you will have a lot of good reasons for it. C# is used for many purposes, in many different contexts, and by many different people with largely varying theoretical backgrounds. Additionally, there sometimes is considerable time-pressure in real life projects. So, if there's the opportunity for abuse, it will inevitably happen!

One of the most cited advantages of the dynamic type is the fact that it greatly eases the interaction with a dynamic language like e.g. Ruby. So code like this...

var runtime = IronRuby.Ruby.CreateRuntime();

ScriptScope scope = runtime.ExecuteFile("SomeRubyClass.rb");

var operations = scope.Engine.CreateOperations();


Object someClass = scope.Engine.Execute("");

operations.InvokeMember(someClass, "do_something", "someArgument");

...can now be written like so:

var runtime = IronRuby.Ruby.CreateRuntime();

ScriptScope scope = runtime.ExecuteFile("SomeRubyClass.rb"); 


dynamic someClass = scope.Engine.Execute("");


Now this looks like normal C# code, right? But do we really want potentially unsafe code look like any other code? Shouldn't writing such code be a bit more inconvenient to the programmer by design, reminding him of the fact that such code is inherently dangerous (and there's no way to make it safer with unit tests or some sort of static analysis) ?

Certainly, MS introduced dynamic to satisfy a popular demand that was very frequently uttered. But the collateral damages can be quite serious, by far outweighing  the advantages. This well may become another proof of an old proverb: "Beware of what you wish for, you might get it."...

Posted on Wednesday, September 23, 2009 6:37 AM General programming/C# | Back to top

Comments on this post: 'dynamic' ? - Be afraid, be very afraid...

# re: 'dynamic' ? - Be afraid, be very afraid...
Requesting Gravatar...
I believe that one of the benefits of using the dynamic keyword is interacting with the dynamic languages like Ruby or IronRuby.

There are rare cases when you want to jump from C# and call Ruby library and process the result in C#. At first that jump across the boundary will be a big performance hit since initializing an IronRuby engine takes some time. But then you might want to call a Ruby library because it is the only available implementation.

A quick example might be some screen scraping API by Ruby that is easy to use. Instead of building the same API using C# you can simply call the Ruby API process the results and send it back to the .NET application.

dynamic is also useful when communicating with COM API's.

I believe that with great powers lies great responsibility!

Left by Mohammad Azam on Sep 24, 2009 5:12 PM

# re: 'dynamic' ? - Be afraid, be very afraid...
Requesting Gravatar...

I absolutely agree that it can be necessary and useful to interact with some sort dynamic language runtimes or COM. It's just that I refuse to make it an everyday, first-class feature of the C# language.
I'm not concerned about the proper use of 'dynamic', it's the misuse of this feature that makes me shudder...

- Thomas
Left by Thomas Weller on Sep 24, 2009 5:37 PM

# re: 'dynamic' ? - Be afraid, be very afraid...
Requesting Gravatar...
Hi Thomas,

In my presentations I often say that you better have a darn good reason to jump from CLR to DLR! At this point IronRuby 0.9 a jump is pretty expensive operation. Hopefully, this will be controlled before 1.0 version comes out.

Left by Mohammad Azam on Sep 24, 2009 7:04 PM

# re: 'dynamic' ? - Be afraid, be very afraid...
Requesting Gravatar...
I think your worry is over exaggerated. First you need to factor in this will be a brand new feature in C# 4.0 it will also seem to be very mysterious on what it does to a less knowledged developer. I find it very unlikely that developers will start using it without understanding what it is. The dynamic keyword is an important step for C# to take to be the end all best language of software developer. Currently the lack of the dynamic keyword has been replaced with dynamic IL injection or emission and other techniques that are overly complex to achieve the lacking of this needed feature.
Left by Chris Marisic on Oct 12, 2009 7:55 PM

# re: 'dynamic' ? - Be afraid, be very afraid...
Requesting Gravatar...
unfortunately this is not a new feature but the rebirth of something that we already had with VB up to version 6.
And my experiences with it were very bad. I was quite happy when it disappeared in the mists of time...
Left by Thomas Weller on Oct 13, 2009 9:27 AM

Your comment:
 (will show your gravatar)

Copyright © Thomas Weller | Powered by: