Wednesday, August 19, 2015

Dynamically vs Statically Typed Languages

There's a huge divide between programmers that like dynamic languages vs those that like static languages. The usual argument for dynamic languages is that it makes it so much easier to add things and change things. You don't need to bother with changing the types of things. They just work.

I strongly prefer statically typed languages now, but I used Python 2 for a number of years. Coming from C/C++, it was awesome. I loved the conciseness, and I didn't have to worry about declaring variables before using them. After my first 1-5AM debugging session where I mis-capitalized one variable, I began to waver in my position. After a few more debugging sessions where I wondered why my properties are undefined, I was beginning to hope for a better way. But I couldn't give up lambdas and list comprehensions. It was so much easier to describe things.

Fast forward a few years, and my first job was at a .NET shop. Although they used VB.NET, I fell in love with IntelliSense. It told me what was available and it was right 100% of the time, regardless of how I had made a typo. Back in Python land, I always had at least one browser window open to the docs to make sure I spelled everything correctly. I had just thought this was the way things worked. After getting IntelliSense, I don't need to worry about spelling anymore. Occasionally, I can even use a library without even reading the docs first! The types and names told me everything I needed to know, right there in my IDE.

Now I use a mix of .NET on the backend and JS on the frontend. When I need to rename something in .NET, I can use ReSharper and it'll just do everything for me. Sure it takes 30 seconds, but I'm chilling those 30 seconds. When I want to rename something in JS, I do a find all through all my files and go through each line to see if it's something I need to rename. If I want to change Name into FirstName and LastName, ... well, there are 1000 references. I say screw it, that feature is too expensive. They'll never split their names.

A huge annoyance with the loosely typed world of JS is how it tries to have method overloads, but in a really clunky manner. I would prefer a bunch of different function names, but the community has embraced a few idioms that mimic overloading. I am glad TypeScript is gaining momentum as we build larger systems out of JS.

There have been a few times when I thought a dynamic language would be good. When I'm prototyping a small program, it is pretty annoying to have to declare classes for everything. I think languages with more aggressive type inference than C# would probably help me out, here. C# doesn't allow me to have anonymous types across method boundaries. This is a huge pain in the butt. Even ReSharper doesn't make it completely seamless. I still have to think of a name when I generate a concrete type for the anonymous type. Unfortunately, these little projects grow to something bigger rather quickly, so I prefer to take the productivity hit (or use dynamic types in .NET) initially.

Another reason to use a dynamic language is that you can get macros with it. Sure some static languages have a little bit of macros sprinkled in, but nothing as awesome as Lisp. Unfortunately, it's impossible to get that level of language defining macro with static types. Several people are working on something close, but it's still icky. At the extreme end, you could pull some stuff from the network and have it generate something. Well, there's no way I can know the types ahead of time. I haven't seen many compelling reasons to use this aside from DSLs, so I don't really want to give up my static types for this.

For the near future, I'll be reaching for statically typed languages first.