Why Static Typing

The case of static vs dynamic typing is a long lasting debate without a clear answer. A few years ago it seems that all of the popular programming languages were dynamic: Ruby1, JavaScript2, Python3 etc but all of the languages I have listed have some sort of static typing added to them. Why is that?

Prevent common typing bugs

It happened to all of us, we accidentally passed a string to a function that expect an integer and if we were lucky, it would have raised an exception and we fixed it right away but if we weren’t so lucky and used a weakly typed language like JavaScript the function didn’t complain but we got an unexpected behaviour down the road.

A type system would have caught most of those problems at “compile” (or linting) time and helped you shipped less buggy software to your clients. Of course there are many other types of bugs that static typing won’t catch but it will reduce your bugs significantly4.

Better performance

A statically typed language can help a compiler generate better code because it have more information about the code. Type hinting in dynamic languages like PHP won’t make the code faster because the only way to validate the types is at runtime which will actually make it slower.

Enable better tooling

Just like the compiler itself, the language tools like it’s completion library or jump to declaration tool can use the added information of the types in the code to know better what to do. For example if you will add type hinting in Python for the class you expect to get in the argument you will have all it’s method in the auto completion pop-up and you will be will be able to jump to the implementation of a function you called because the IDE/editor will know what class you called.

I think this is one of the most important points in favor of static typing that is often overlooked and when you realise that you need it, it will probably be too late.

Document your code

Another small but nice improvement of statically typed code is that the code really document itself. Of course static typing won’t replace all your comments but in dynamic typing you often see something like JSDoc or docstring that litter your code with with verbose comments about the function arguments and return type. All of that can fit in a single line of the function declaration like with TypeScript and Python 3 type hinting.

Conclusion

In conclusion I personally think that static typing is the right way to go. Dynamic typing with optional type hinting like TypeScript and Python 3 are a nice middle ground if you feel that statically typed language are too restrictive.


  1. It was recently announced that Ruby 3 will have static typing. ↩︎

  2. JavaScript itself doesn’t have static typing but TypeScript is gaining popularity in the JavaScript community. ↩︎

  3. Python 3.4 have introduced type hinting to the language but without built in type checking. ↩︎

  4. To type or not to type: quantifying detectable bugs in JavaScript ↩︎