Hacker News new | ask | show | jobs
by wk_end 118 days ago
Yeah, the idea that types exist just to help the compiler catch your mistakes shows a depressingly superficial understanding of the benefits of static typing.

Types exist so that the compiler can reason about your code better - but not incidentally, they also help you reason about your code better!

To wit: even when working in dynamic languages, it's often considered a good practice to write down in docstrings the types of objects a function can operate on, even without static enforcement. Thinking about types is helpful for humans, too.

And it's not even just a thing to help you read code in the future - types help me write code, because as I sit down to write a function I know the possible values and states and capabilities of the object I'm working with. In the best of cases I can analytically handle all the possible cases of the object, almost automatically - the code flows out of the structure of the type.

2 comments

> Types exist so that the compiler can reason about your code better - but not incidentally, they also help you reason about your code better!

THIS. So much. This observation is extremely intuitive to me.

Having inherited some large python code bases before type annotations were common made me never want to personally read through highly-typed inferenced code again.

It reminded me of a mathematician in my field who had rather brilliant ideas, but whose papers were largely unreadable due to idiosyncratic symbology and style. Fortunately in that case one can still leverage an information dense symbology that points to a well-specified formalism.