Today I read on C# Frequently Asked Questions about "What is the difference between "dynamic" and "object" keywords?" This sent shivers up my spine after I read about the new .NET 4.0 keyword "dynamic." What's next a .NET Option Explicit On?
Alexandra Rusina starts off by talking about Object, and that's cool, we should all know about that one, right? Right? RIGHT!?!?!?!?! OK, ok, you know about it (and if you don't, you really should look here). The key here is Object lets you defer some decisions on variable types until execution/runtime but still ensure type safety at compile time. In other words, you can basically fudge some of your flexible code to compile while still ensuring strong typing. BUT when you goto use those variables, you will EVENTUALLY have to cast them. The deal here is, in the .NET world, you can pass variables around pseudo-generically (as an object, ie ArrayList.Add(object)), but eventually you have to pay the piper, and figure out what variable type you're dealing with to cast it. And if you are wrong, you'll be looking straight down the barrel of an InvalidCastException (well, either you will, or QA or worst yet, your customers, DOH!).
Well, around sixth paragraph, Alexandra says......and I quote......"Here is where the new dynamic keyword in C# 4.0 comes in. It tells the compiler not to enforce additional rules upon your code."
Did you get that last sentence? It "tells the compiler NOT to enforce additional rule upon your code"????? Doesn't that sound like the variant keyword in VB? Isn't that so 1990s? WTF?
Ok, I'm not coming down on Alexandra, she's just explaining the keyword, and doing a good job of it. But I have to ask, what made the .NET language folk think this was a good idea? Especially when we ALREADY have the object keyword? In fact, the variant keyword was one of the most important keywords DROPPED with VB.NET! This smells a lot like we're going right back to the GOTO statement in BASIC IMHO!
In all fairness, .NET is still doing all the runtime checks BUT AT RUNTIME ONLY!!!!! What the dynamic keyword lets people do, is DELIBERATELY and INTENTIONALLY bypass all compile-time checks and not finding casting errors at compile time. Uh, when is it the cheapest time to find bugs? At customer execution runtime? DUH.....NO!!!!!!!!!!!! The sooner to implementation time, the cheaper it is to fix bugs. Ask any QA person, they tell ya!
So why is .NET itentionally putting in something which let's devs bypass the compiler? Hey, maybe MS has their reasons for doing this, but I tell ya, we've survived until now (.NET 1.0, 1.1, 2.0. 3.0 and 3.5) without it, why do we need this NOW? My suggestion is, if you're using this keyword, either you EXPLICITLY KNOW WTF you're doing and thought about NO OTHER solution, or you're comletely clueless.
Now that we're a little bit more aware of the dynamic keyword, I hope you will stay away from it! Now it's time to grab a coffee and get coding!
Resources:
C# Frequently Asked Questions: What is the difference between “dynamic” and “object” keywords?
MSDN: dynamic (C# Reference)
TechRepublic: Get the most ouf of Variants in VB6
TechRepublic: The Top Five Changes between VB6 and VB.NET
stackoverflow: GOTO still considered harmful?