Search
Friday, March 12, 2010 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  
Feb 2

Written by: Peter Henry
Tuesday, February 02, 2010 8:52 PM

Variant variable typeToday 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!).

Goto comicWell, 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?

Copyright ©2010 Peter Henry

Tags:

3 comments so far...

Re: A variant by any other name, IS STILL A VARIANT! Didn't we learn our lesson the first time?

If you use this keyword, IMHO, in the immortal words of Russell Peters' father, "Someone gonna get a hurt real bad!"
http://www.youtube.com/watch?v=vVlX9Ve5Rv4&NR=1

(Actually he's a great comedian, and even if you don't use that keyword, it still a funny vid to watch over lunch!)

By phenry on   Wednesday, February 03, 2010 10:02 AM

Re: A variant by any other name, IS STILL A VARIANT! Didn't we learn our lesson the first time?

I'm still holding out judgement on dynamic for now. I think it really comes down to personal preference. Some people like format structure and a rigid, clear code (ie, 'me'). Other people can tolerate a degree of ambiguity and trust that the application/programmer knows what they're doing.

This is one of the reasons that once I got my head around Ruby, I decided that I just didn't like it. Duck-typing just didn't agree with the way that my brain works when I'm writing code.

By Peter Bernier on   Wednesday, February 03, 2010 9:52 PM

Re: A variant by any other name, IS STILL A VARIANT! Didn't we learn our lesson the first time?

Thanks Peter for the comment. I understand about the personal preference thing, and I also understand, there are some cases this is the only screwdriver that'll fit. BUT!!!!!!!! I'm just afraid of the noobs who are going to create some LOB app and use this "cause, oh well, it compiles." I worked at one place where someone commented out a bunch of unit tests, "ran them", and was content when his "unit tests" all passed. WTF? DOH!!!! :<

Duck typing? I gotta google that one, I've heard it before but never knew what it meant.

By phenry on   Wednesday, February 03, 2010 9:56 PM

Your name:
Title:
Comment:
Add Comment    Cancel  
     
  
Copyright 1999-2008 by PCHenry.com   Terms Of Use  Privacy Statement