So, yesterday, I posted what I believed to be a fairly banal post about the benefits and the possible dangers of using the new dynamic type in C# 4.0.
Ultimately, when I encounter something new like generic types, I try my best to approach the foreign object (pun intended) with a touch of negative capability. Whether I achieve anything worth reading in my quest to do so, I’ll leave that up to my patient, understanding readers (that’s you).
Anyways. One fellow was kind enough to link to my post on reddit. Almost immediately, another kind reddit fellow commented on the link with this tidbit of genius:
Hmmm….what constructs can I use in my C# to reduce maintainability, and make the next programmer’s life a living hell….
Woo. Them are some strong words.
And that’s not the only anti-dynamic sentiment floating around in the .Net ether, either (homonyms!). C# dynamics (I use “dynamics” here as a shorthand for “dynamic typing”) has, it seems, firmly vibrated many of the proverbial sticks implanted in many-a professional developers’, eh, caverns of anti-pleasure. For one, David Nelson writes:
C# is based on static typing for a reason; tossing that out the window for the sake of the latest fad is irresponsbile [sic] at best.
Here’s another common sentiment, as espoused by Coder4Hire:
Comparing the number of keywords and programming constructs of C# to C/C++, Python, etc. and you will see how bloated C# has become.
And another, by Francois Ward:
There are dynamic languages that are vastly superior to C#, if you want dynamic. C# was meant to be a “pure” language.
And another, from terse “Joe“:
Don’t do it.
And another, which I made up, just to be cooler and terser than Joe:
Why?
—
Let me be clear: I’m not trying to be a flame diva here. I mean, I think I understand the fear. I really, honestly do. Something about the concept of a “dynamically static” object makes me want to crap my pants and crawl into bed. But why? I mean, what’s wrong with me? With us? Dare I say it–but what’s the worst thing that could
And besides, just as the old saying goes:
Guns don’t kill people; people kill people.
Couldn’t one similarly argue that:
Tools don’t make programmers crappy at writing testable, maintainable code; programmers make programmers crappy at writing testable, maintainable code.
?
—
Let me hone my dynamic/meandering post and just say this: whenever there’s a conceptual argument like this one, it’s probably the case that both parties have some sort of a justifiable position. And I do honestly think that there is a justifiable case (or cases?) to be made against the existence of the dynamic type in C#. But, in reality, beyond the realm of such conceptual argumentation (which I love to partake in), the truth of the matter is: the dynamic type in C# exists. It does. Time to move on.
In other words: the baby’s born, it’s kicking, no need to waste our breath arguing about whether or not we should make sweet love.
So, now what? Let me start by saying this. And I say this from the bottom of my heart, because it will be the mantra that I will chant to myself whenever I start to type the “d” in “dynamic”:
If you can easily accomplish a task without resorting to the dynamic type, do it. If you catch yourself searching the internet for ways to do x via dynamics; first try to find a solution for x that doesn’t involve dynamics. Better safe than sorry.
To put this another way:
Using dynamics is like borrowing your dad’s car. It’s faster, cooler, awesomer [sic]. But you better have a really good excuse for driving it.
Even C-daddy Jon Skeet, i.e. demigod of light and braveness, isn’t exactly sure why one might resort to dynamic typing. That inevitably raises the question: why do you, Mr. whoever you are, think that you need to use the dynamic type in your project? Are you smarter than Jon Skeet? Or are you too stupid to know what Jon Skeet would do (WWJSD)? That’s right. Retract in fear… I know I would (IKIW).
Speaking of Jon Skeet, Craig Stuntz has a great response to Skeet’s post, Where Do You Benefit From Dynamic Typing. I’ll include Craig’s comment in its entirety, as I think it’s a helpful dose of insight:
Some food for thought (you can draw your own conclusions):
1. Some problems are inherently dynamic. E.g., web requests. You can’t compile the internet! MVC uses reflection for this today. I think that dynamic typing is a more expressive approach to the same problem.
2. The expressivity I mentioned above is itself compelling: Reflection introduces a strong dependency on a mechanism. Dynamic invocation says, “Here’s my intention; please do this in the fastest way possible.”
3. Higher order types with complex constraints can be intimidating to mere mortals. Read the Scala spec for an example. Even C# “pros” frequently don’t understand the subtleties of contravariance and covariance, and frankly C# gives you only the “easy” parts of higher-order typing. By contrast, dynamic typing “just works.” When it works, that is!
4. Static vs. dynamic typing is not the be-all and end all of program correctness. They are part of a spectrum of tests over time which include, but are not limited to: Compiler checking, unit tests, assertions, static analysis, runtime verification, etc.
Clear, concise, friendly. Damn. I wish I could write like that.
In summation, then, the mild controversy surrounding C# dynamics surely implies one of two forthcoming truths: (a.) that the dynamic type is going to lead to some truly, madly, mindblowingly awesome code (i.e. the true slim shady will stand up), or, inversely, (b.) that the dynamic type is going to lead to some mindblowingly late nights–nights spent drinking Mountain Dew and fixing crappy, crappy code.
Either way, it’ll be a ride, my friends. A good one or a bad one. And if the ride gets too bumpy for you, never fear. There’s always Ada.
Read More
You can leave a response, or trackback from your own site.
6 Responses to “Dynamic Type in C# 4.0: The Cool New Thing To Crap On”
Leave a Reply



Hm, dynamic is an add-on. There is no compulsion from anyone
.
If you find smart ways of using it, use it. Obviously, there are areas where dynamic is going to make things easy. It is certainly a better choice than using reflection. Or, say, what about creating a fluent wrapper on top of xml or json. Like this
http://amazedsaint.blogspot.com/2010/02/introducing-elasticobject-for-net-40.html
You need to be careful with posts like this–you could easily get a reputation for being reasonable, rational, and logical.
Nicely stated!
rp
Anoop–
Your ElasticObject sure sounds dirty, but is nonetheless amazing. A really great example of the kind of TESTABLE, usable tomfoolery you can pull off with the new dynamic type.
Cheers,
Evan
Well, I think I love the new dynamic capabilities. After working with anonymous types, I found lots of problems.
You cant send anonymous types outside, then why there is such capabilities. I think dynamic will allow such.
Yes reflection is also another field that might benefit with dynamics.
I would like to up-vote this capability.
[...] – Dynamic C#http://www.weirdlover.com/2010/05/26/c-dynamics-the-cool-new-thing-to-crap-on/ [...]
[...] – Dynamic C#http://www.weirdlover.com/2010/05/26/c-dynamics-the-cool-new-thing-to-crap-on/ [...]