C# dynamic: A bad idea?

It seems like the most talked about thing on the street for code monkeys coming out of PDC is the introduction of dynamic typing. I'm trying very hard to understand why this is a good idea. Nikhil Kothari makes a good case for it interacting with Javascript from Silverlight, but that strikes me as a fringe case. I guess there's a part of me that is curious to know if this came out of a science project or actual customer need.

With all of the mucking around I've done in Javascript the last two years, given the various AJAX frameworks, I can't say I'll ever be particularly happy about the lack of real types. It's just such a pain to debug. And in C#, ever since the invention of generics, which still clue in the compiler and Intellisense, dynamic typing seems even less useful.

Maybe my biggest fear is abuse. While I've worked in very good shops with solid code review practices and some decent ground rules on coding standards, I've also seen the crap out there. I still cringe at things like DataSets and DataAdapters.

Perhaps my fears are unfounded. I thought using the var keyword was a recipe for death, and now I can't imagine not using it. In the mean time, it'll be interesting to see the various computer science geeks debate this one.

10 Comments

  • COM interop, especially for Office apps, is another huge use case for the dynamic keyword.

  • All it is duck typing for C# which it really needs in order to make the DLR and interop first class citizens in the language.

    In the end it will make interop accross everything look and feel like real c# even though you may be missing intellisense.

    I also think we will now see cool usages with mixins and proxies that were previously very difficult to achieve because of having to generate IL at runtime.

  • Joe,

    Morphing C# towards a dynamic language for the sake of COM interop seems to me like treatment of symptoms. I must admit I never work with COM interop, but it looks to me that what's missing here is an intermediate .NET API that hides the real calls to the ugly Office COM interfaces. Microsoft should not force that C# team into those evil dynamics, but let the Office Team clean their own rubbish. The Office team should release a managed Office API that hides those methods with optional parameters. This way the C# team could continue to follow the path of purity and divine verification (do I hear a Spec#).

  • The best explanation I've found is at Eric Lippert's blog which includes 2 videos with Anders explaining it. Here's the link. Worth a watch:

    http://blogs.msdn.com/ericlippert/archive/2008/10/29/the-future-of-c-part-three-on-video-plus-the-dead-trees-edition.aspx

  • This reminds be of VB6. I do not like it because of the abuse possibility by coders who prefer hacking code out without proper structure and thought.

    The more data/type checking you make the computer do at runtime the more likely it is to have major bugs, security gaps, or performance issues.

  • "This reminds be of VB6. I do not like it because of the abuse possibility by coders who prefer hacking code out without proper structure and thought."

    And yet there's a lot of hacked code that makes $$$. Hardcore coding fanatics are going by the wayside in favor of ones that provide business value. Perfect code has no business value except to software framework providers [Microsoft .NET] versus hack-code like the Office API.

  • No business value? Are you kidding me? While perfect isn't necessary or realistic, following standards and practices is what gets you out of dodge in the long run. Do you work with code that you write and no one ever touches again?

  • Eric probably does write such code...

    I think making C# support dynamic types is indeed a feature, as we ultimately don't have to use it if not needed. But as always, it will just allow for more coding horrors, by people like Eric Newton above.

    100% agree on the wrapper over office API.

  • Poorly written code makes $$$ in spite of its quality, not because of it.

  • I'm prefer the power of different paradigms
    in c#. The excellences of dynamic programming
    are incredible if they are well understanded.
    I'm sure that COM interop it's an excuse, to
    don't admit a obvious thing: modern languages
    can't being totally static typed in all
    situations but in some situations they have be
    more flexible to permit a natural coding in
    dynamic oontexts.

Comments have been disabled for this content.