All roads lead to static typing

Note: Just in case you need a quick refresher, wikipedia has a nice description of strong and weak typing.

In recent years I became a firm defender of Python static typing (via type hints and MyPy, more details at this blog post). It is not that Dropbox, Stripe, Microsoft, Facebook and many others are creating immense projects around dynamically-typed languages, I've also worked with big and sometimes codebases in a variety of languages, and I've seen first-hand how difficult and mentally exhausting is to understand code when there are no type hints, how error-prone it is no matter the amount of tests you add (many of them superfluous and just palliating the fact that any variable can hold anything) and how your IDEs and tools can barely cope with those big codebases (even if it's a robust one like PHPStorm, RubyMine or Visual Studio Code). For an early startup of course you go faster with a dynamic language, I love Python because I get what I want with so few lines of code, but I would probably never ever use it "naked" anymore for anything serious if the plan is to evolve it and work with other people.

Now that I'm working with Java I'm back to real compiled languages, after more than a decade using dynamically typed ones: PHP, Javascript, Ruby and Python. And one thing struck me from that list... all of them have evolved into supporting static typing one way or another, and most directly inside the language core.

PHP introduced type hints in PHP 5.1, but really until version 7 didn't have enough primitive declarations. HipHop compiler was an amazing idea to speed up PHP: Simply compile all of it into C binaries, and now there's a full virtual machine (HHVM) making it more akin to Java than C++, although it's diverging into Facebook's Hack language so it's no longer "pure PHP".

Javascript is a curious creature. I guess the community is reluctant to adding types because being so old and evolving so much in other areas, typing is still weak and dynamic. But that didn't stop folks from adding types with TypeScript, a compiler transpiler that adds types and generates "typed javascript". I haven't personally used it, just seen and reviewed code written with TS, and it looks great. For sure adds more "boilerplate" but no more than for example React adds when you're building a simple form compared with an old HTML + JS implementation.

Ruby until recently only had external type checkers, like Sorbet, but it seems that finally Ruby version 3 will include RBS, a new language for defining types and some related command line tools.

Python has had type hints since version 3.5, optional and allowing also "gradual typing", but with the deprecation of Python 2 its gaining a lot of traction. The benefits will be broader once specific compilers (like mypyc) get more mature, as then Python modules would be able to be compiled into way faster C extensions (much like PHP's HipHop principles).

I think that this static typing convergence of many development languages is just one way of dealing with bugs and errors in long-lasting projects. Code linters and auto-formatters (example for python) are another way, dealing in this case with the human factor: Nobody will remember always all the rules, or will spend time nitpicking instead of focusing on real code issues, so better to enforce them automatically. Other more radical approaches are directly new languages: Both Go and Rust were born to try to fix C++ problems, and Hack derives from PHP.

In the end, all seems to circle around the same: Trying to build robust software in a scalable way requires certain development language guarantees and safeties.

Tags: Development

All roads lead to static typing article, written by Kartones. Published on