Static Vs Dynamic Typing

Between work and personal projects, I spend a lot of time jumping between Statically typed languages (primarily C# and Java), and dynamically typed languages (Perl, Python, JavaScript, and VBScript). Even when I ignore the (minor) problems inherent in working with so many different languages (primarily centered around having to context switch between the syntaxes), the biggest headache I continually run into switching between these languages has simple to do with the type system.

Perhaps it’s because I cut my teeth on C, which despite appearances is really a dynamically typed language, since type names do little more than tell the compiler how many bytes it should be addressing at that moment in time, or maybe it’s the fact that much of my programming time is spent on the web, where really, everything is a string. I’m not sure I buy that one, though, most of my programming in College was centered around binary data, and I used to hack proprietary file formats for fun.

Maybe I’m just getting lazier, but constantly having to manage types is really getting annoying. This is a little worse today than it’s been because I’m finally getting back into Android programming, and I’d forgotten how much manual type coercion Java really forces you to do that C# handles on the backend for you. If it’s not already obvious, I’m definitely in favor of Dynamic type systems. The just make for easier code to write, and easier code to read.

At the moment, I’m working on an application form in ASP.NET MVC using C#. Not terribly difficult, but I’m having to write a significant amount more code in C# than I am in the project JavaScript (I duplicate the code in both places for a better user experience). This is mostly because of the need to explicity convert types to numeric types to test that certain fields (such as Social Security Numbers) are entered in correctly. It’s just becoming a simple fact, that dynamic languages allow us to write code faster. There is always the potential that we can write worse code faster, but with a little care it’s just as easy to write better code faster.

Now, while I definitely prefer dynamic typing, there is the valid performance argument. Dynamic typing is slower. This is a fact. However, for most uses these days, it isn’t noticably slower. Computers are fast enough today that the overhead involved is negligible for almost all applications, but there is one place where static typing is still generally preferred. Mobile Computing (primarily phones) require static typing, not so much because of the speed of the application execution, but because the battery life would be negatively impacted by the constant messing around with determining (or modifying) types.

However, that doesn’t mean that I can’t program as if the types were more dynamic. C#, particularly in later versions, has implemented a ton of compiler hacks which make C# seem more dynamic. The ‘var’ keyword, inline property setting and dictionary construction, plus quite a few more things, all making the code easier to write, while keeping the language static. I’d be a lot happier with Google’s choice of Java for Android if Java would implement even a small number of these more ‘dynamic’ features. Perhaps I should just put together a C# compiler that spits out Davlik Bytecode (I wonder how modular the Mono compiler source is?).

Well used, dynamic typing can lead to much cleaner code, much faster. I’m very glad to see the trend in Software Engineering toward dynamism, even if it frankly could come much faster than it is.