I'm a web developer who is currently using mvc3 razor and Umbraco to create web applications. I've not been in this job long, and have moved over from using dynamic to strongly typed elements to follow standards.
Ive never really been given much of a reason, and searching online it seems clear that its much easier to follow and debug set type variables, but I'm confused about using it in functions.
For example, when using Umbraco Content, I've now switched to 1Umbraco.TypedContent1. Im guessing that it returns a strongly typed variable, but are there any other benefits to it other than debugging?
I came from a primarily PHP and Python background, but switched to C# about 3 years ago. Switching to primary development in a strongly-typed language like C# was admittedly a learning curve. At times it can feel overly restrictive, especially when combined with C#'s very rigid inheritance. However, strongly-typed languages buy you a number of things.
First, and probably most importantly is compile-time errors. With dynamic languages like Python, there's no indication that you've used the wrong type until the application is running and you get a runtime exception. With a strongly-typed language, the application will not even build, so you generally don't end up with timebombs in your code.
Secondly, strongly-typed languages can be highly optimized. By ensuring that each thing is always a particular type, the compiler can optimize memory allocation and the actual compiled assembly code.
Thirdly, strongly-typed language encourage good programming practices. While it can feel somewhat restricting, strongly-typed languages force a structure to your code that can often be lacking in dynamic languages. That's not to say it's not possible to write good code in a dynamic language, of course, but it does mean that when developers do not care to take the time or attention to detail they should, far more damage can be done in a dynamic language than one that is strongly-typed.
That said, C# is not all strongly-typed. Namely, there's the somewhat odious dynamic
keyword, that's abused far to often, and MVC almost encourages the use of dynamics with things like ViewBag
. While dynamic
can have its place, I'd recommend avoiding it as much as possible, as you're essentially throwing away all the benefits of working in a strongly-typed language.
There's also the concept of generics. You're probably using generics without even realizing it, but if you're feeling overly restricted in your programming, you should probably look into them more and learn how to really take advantage of the power they bring. It's a bit like having a dynamic type, but it's still set at compile-time, so you don't lose any of the the benefits that brings.
In addition to the stuff that Chris mentions, there's also a performance benefit, as the dynamic API in Umbraco is slower than the strongly typed one.
Also, dynamic types will be removed in Umbraco v8: http://issues.umbraco.org/issue/U4-8626