3 ms·
> Why not let the computer do the work? Because a program's types can serve as part of its documentation.
by MaxBarraclough 6y ago
> Why not let the computer do the work?
Because a program's types can serve as part of its documentation.
- mathgladiator 6y agoThis is under-appreciated, and as I have aged I have discovered that I prefer the verbosity over brevity. Namely, when mentoring more junior engineers who can barely write code at all.
- deleted 6y ago[deleted]
- hombre_fatal 6y agoThe other benefit of top-level/function annotations is that they ensure that your mental model of the code is consistent with what the code actually does. So many times I will accidentally return 42 or Result<Result<42>> when I mean to return Result<42>. With annotations, my error is instantly obvious. Otherwise I accidentally have a function that returns Result<Result<number>> and it may lead to a very weird error message when I use `foo().map(_ + 100)` somewhere else. That's letting the computer doing more work, not less of it. Relying too much on inference can bow back into the human doing more of the work to figure out what the types actually are in a given spot.
- lmm 6y agoSometimes an IDE hover can be the best of both worlds - the information is there when you want it (and guaranteed to be accurate since it's using the language compiler's own type inference), but not taking up space when you don't want it.
- eru 6y agoThat helps, but it's orthogonal to adding type annotations. The computer can only tell you what types your code _has_ (from inference), but not what type it _should_ have based on your intentions. Type inference can only complain when your code contradicts with itself (on a type level), but can't tell you when it contradicts with your intentions. If you don't write out your intentions, you'd have to hover over everything to see if the inferred types differ from your expectations. IDE hover types and collapsible type annotations (and alternatively also collapsible implementation) are still useful as a way to declutter your UI when reading code.
- mrmr1993 6y agoIt doesn't always make sense to plan the types ahead of time; letting the compiler do the work while you code and then creating a nice interface after is a reasonable way to work.