Why Only Type-hints?

Whether you're working with Javascript / TypeScript or in Python, type annotations are a thing. On Hacker News we've seen big debates on whether types improve code quality or are just cargo-cult slowing us down. Now here's the thing: I've come to believe there's something very backwards in our approach to type hinting. Let me explain why.

Historically most languages were designed with types baked in. Think C, Fortran and probably most languages one level above Assembly. Types are a simple idea and were needed so that the compiler could figure out what operations to perform, e.g. the + operator has defined behavior on integers, and another on strings. With dynamic languages such as Python or Javascript, types were handled in the interpreter at runtime and no longer necessary in the language itself. In the last decade, with the rise of TypeScript and Python type hinting we're adding types back, not because the language needs them, but so that tooling can help us with the correctness of our software. I believe adding type hints helps with that but why are types the only language affordances to add when we are trying to improve correctness? We should be asking ourselves a different question: Are most of our software problems actually with types? I believe we are short-sighted in that we simply try to add features we've already seen in other languages. We are thinking inside the box.

If I look at most problems in the codebases I've managed, I've seen errors in performance, side-effects, security, etc. To be honest, types or not the first thing on my list. Now why do type hints improve quality? They allow the compiler to reason about the software that we make and it helps us to check our assumptions, faster than any human could do. We should allow our programming environment to help us with other aspects too. Here's an over-the-top example for Python:


When we put our expectations down, the system can help us with the following kinds of problems:
  • Performance: a method was slow on the production set, but another developer didn't know and used it in the UI anyway.
  • Side-effects: developers are not sure if functions are pure or if it could lead to external APIs being called, so how do we handle that if there are database rollbacks?
  • Security: it's not clear who is allowed to execute this function, is it only for system administrators, customer service employees, or end-users?
  • Data Privacy: Is there any PII going into the data lake that is only supposed to contain anonymized data?
  • Memory: This function handles incoming video files and buffers them in memory. It will break when someone uploads a 1GB video file to your tiny server.
  • Intent: the intent of the original developer is completely unclear. We can use free-form comments and things like pydoc for this, but it's a horrible solution in my view.
  • Tests: test systems are add-on tools to our languages, there's nothing in the language itself that allows us to ensure input/output or other kinds of tests.

Now obviously this is ugly. There's no inference or other clever things, and it builds on the annotation functionality in python which makes it only available at runtime. We want it when we are writing our software. I'm not proposing this syntax, but I do want a programming environment where we can add other hints than just type hints. It will probably never fit on one line like current type annotations do. But I do believe we will find solutions for this in the future.

Comments

Popular posts from this blog

The software that I love

Things I Didn't Build Yet

The world's first Cowboy key-fob