Static, Dynamic, and DNRTV, oh my

I recorded a DNRTV with Carl Franklin a little while ago, and one of the viewers, Kyle Szklenski posted this comment:

I listened to the DNR TV today with Bill Wagner on dynamic typing. I was really, really, ecstatically happy to hear fellow champions of static typing. It's all too often that my coworkers or other people online tout the benefits of making everything in your application dynamic, and it makes me physically ill to have to listen to it. Hyperbole aside, I think that if you cannot design a system to work with static typing, then there's probably something wrong with your design sense. Thanks for the awesome presentation, and am looking forward to hearing Bill discuss the new async stuff!

I’m not sure I’d phrase it that strongly. It’s true that C# is a static language. Every feature in the language emphasizes a belief in the benefits static typing. It is true that you could design any system in a static language, but that doesn’t mean it’s the best way to design any system. The features added in C# 4.0 demonstrate that.  There are idioms where having dynamic typing is a great benefit. COM interop, runtime type checking, and language interop are obvious examples. Without adding language features, these idioms require pages of code.

That’s work to write the code.
That’s work to test the code.
That’s work to maintain the code.
That’s pages work to debug and extend the code.

Language features that shrink those pages to a few lines (with the same features) should be applauded.  We get done faster, with fewer bugs, and have more readable code.

However, adding dynamic types to your program has a cost. It is a very different style, and the rough edges where dynamic and static types meet can produce surprising behavior for those expecting the familiar. That causes disruption to our thinking when we try to understand programs. It can increase bugs.

For this reason, and many others, I tell customers to view dynamic is a very powerful feature, but one that must be kept in a pen. Use dynamic typing where it helps. Don’t make everything dynamic, and prefer narrow scopes for variables that are statically typed to be dynamic.

Created: 3/4/2011 4:40:49 PM

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.