by robertlagrant · 1y ago · 0 comments
if user := get_user() is not None:
# use user
else:
# return error
Although given the happy path code can mean you don't see the error condition for ages, I much prefer this: if (user := get_user()) is None:
# return error
# use userIt actually looks really natural in python, glad they added.
Easily 60-70% of all go code is about propagating errors to the caller directly.
And spoiler alert, every language propagates errors. Sometimes automatically via exception handling, somewhat simply by returning error values. rust does this too.
Matter fact, even javascript might be creeping toward this model of explicit error propagation soon.
Good, because it is easier to understand.
Although it will flag unused variable. So you will have to make an effort to deliberately ignore the error value.
Still not quite as nice as the compiler forcing you to handle the error case.
But it's a bit orthogonal of a concern. I do have things to say about errors but my complaints are a bit more nuanced and made with hindsight.