Hacker News new | ask | show | jobs
by throwaway13337 641 days ago
Value types with contracts should be the focus here. Specifically, the story should not be of type str, but rather of type story that is only allowed to exist when it meets certain conditions.

This approach ensures only a single point of validation, a lower mental overhead as you are certain it is valid everywhere, and fast failure when it's not - by contract, even.

There are a few places where this concept is discussed, but the most concise phrase is: “Parse, don’t validate.”

Zod (TypeScript) is a good example of a library like this, as it is oriented around types. I’m not sure if Python has something similar.

However, overvalidation can make your code overly verbose, which is something I’ve fallen into before. Use it sparingly.