Hacker News new | ask | show | jobs
by Swizec 898 days ago
> Static typing lets you do more complicated things by offloading a subset of complexity-management to robots

I've read some of the research on this! Yes, static typing improves documentation and helps you navigate code.

It also correlates with code quality and reduces smells. Inconclusive whether that's because of static typing or because more mature teams are likelier to choose static typing.

But all the research agrees: Static typing does not reduce logic bugs. You can build the wrong thing just as easily with dynamic and with static typing. The only type of bug that static typing reduces is the sort of bug you'll find by running the code.

In my experience, static typing is best thought of as a way to reduce the need for manually written unit tests. Instead of writing tests that break when a function signature changes, you write types that break when you call functions wrong.

You still need tests for logic. Static typing doesn't help there.

2 comments

> Static typing does not reduce logic bugs.

This seems like a strong statement to make based on the research. What I've seen falls into several camps:

- research that made some conclusion about logic bugs for complete beginners on small assignments, with languages that have bad type systems

- research that had significant limitations making it impossible to generalize

- research that failed to demonstrate that static typing reduced bugs—which is very different from demonstrating that it didn't!

I haven't done a super thorough review of the literature or anything, but I have looked through a decent number of software engineering papers on the subject. The only strong conclusion I got from the research is that we can't get strong conclusions on the subject through purely empirical means.

Hell, the whole question is meaningless. "Static typing" is not one thing—there's way more difference between Java and Haskell than between Java and Python, even though both Java and Haskell are statically typed and Python isn't. (This is even assuming you completely ignore Python's type annotations and gradual typing!)

> The only type of bug that static typing reduces is the sort of bug you'll find by running the code.

This is a pretty solid argument in favor of static typing, then, unless you somehow have a test suite that exercises every possible code path and type variation in your codebase, and also keeps itself perfectly up to date. Because otherwise you're rarely running all of your code and verifying the result.