Home > Blog > Javascript's Lack of Static Types Improves Code Quality

Javascript's Lack of Static Types Improves Code Quality

5 Feb 2014

EDIT: I’ve learned a lot since writing this post, and strongly disagree with my former-self. See my followup post on why I’m now a fan of strong typing: How I Learned to Stop Worrying and Love Types

Javascript cops a lot of criticism from just about everyone, but dynamically typed languages in general are often considered ‘toys’ by die-hard Java and C# coders. “You can’t build an enterprise application with a dynamically typed language”, they say. There are plenty of arguments for and against dynamically typed languages and I won’t go over them here.

But here’s my issue with statically typed languages: They make it easy to be lazy. You can access the property of an object that is a property of an object that is a property of this object and you can be sure it will be there! Otherwise the compiler would tell you, right?

When writing a Javascript function, you never really know if the parameter you get is the type of object you expect. You’re relying on the good intentions of the caller of your function. Of course you can (and should) check the properties of the object to see if it has the properties that you expect, but you can’t rely on the compiler to catch this at run-time. So why would this ever be a good thing?

The lack of type checking at compile time means you must write small, modular code that only does a few things. If you’re accessing the property of a property of a property of an object there’s a good sign either you need to break things out or you’re some kind of masochist, setting yourself up for long nights of debugging over a bowl of spaghetti.

This mindset has unfolded naturally with the huge eco-system surrounding node.js and npm. Node.js developers seem to favour collections of small libraries (like express.js) over monolithic libraries (like Zend). Namespacing doesn’t really exist in Javascript, so you never end up with something like com.sun.java.util.collections.hashmap.

Writing modular code in Javascript was once an arduous task, but with modern tools like browserify and RequireJS, there isn’t any real reason to avoid it. Large applications can be built in Javascript, simply don’t build them like a large application.

I don't email very often

Powered by Buttondown

Profile picture

Jordan West

Sydney, Australia

jordan [at] west.io | twitter | github | youtube | instagram