14450

Dynamic vs strongly typed elements

Question:

I'm a web developer who is currently using mvc3 razor and <a href="https://en.wikipedia.org/wiki/Umbraco" rel="nofollow">Umbraco</a> 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?

Answer1:

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.

Answer2:

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.

Answer3:

Also, dynamic types will be removed in Umbraco v8: <a href="http://issues.umbraco.org/issue/U4-8626" rel="nofollow">http://issues.umbraco.org/issue/U4-8626</a>

Recommend

  • Eclipse Shortcut to Split Long Strings
  • How to add html to an aspx C# codebehind page?
  • How to determine the length of a string (without using strlen())
  • Get contents of an iframe which has ajax content, with Greasemonkey
  • SELECT Dropdown that redirects without Javascript
  • Converting DatetimeIndex to Timedelta index
  • access an alertView's calling view
  • How can I use scipy optimization to find the minimum chi-squared for 3 parameters and a list of data
  • Why is it still possible to insert a foreign key that doesn't exist?
  • R - Change list of ggplot objects into a list of grobs that grid.arrange will accept?
  • Insert Statement
  • Cannot style mat-tab without ::ng-deep and !important
  • Group variable in cobol
  • R h2o.glm - issue with max_active_predictors
  • AlertDialog style when using setView()
  • xtable - background colour of added rows
  • joining two bezier curves
  • Row to Column conversion in Talend
  • NUnit 3.0 TestCase const custom object arguments
  • Custom preprocessing in caret
  • Can I programmatically choose the Android layout folder?
  • calculate gradient output for Theta update rule
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Does Mobilefirst provide a provision to access web services directly?
  • How can I speed up CURL tasks?
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Moving Android View and preventing onDraw to be called over and over again
  • ADO and msqli connections very slow
  • Android activity accessing service's static reference before the service is ready
  • Why querying a date BC is changed to AD in Java?
  • Atlas images wrong size on iPad iOS 9
  • D3 nodes and links from JSON with nested arrays of children
  • Database structure design with variable amounts of fields
  • Switching to Release Build causes runtime error in Web Reference
  • NetLogo BehaviorSpace - Measure runs using reporters
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • ActionScript 2 vs ActionScript 3 performance
  • Recursive/Hierarchical Query Using Postgres
  • java string with new operator and a literal