News for hackers. Stuff that matters.
Re:We should be more ambitious with the software we build by beecasthurlbow on Sunday October 25, 2026 @03:40PM (#50235311)
KeyError localizes the issue to the lookup (ie “right here this key should be present and it’s not) vs later in the code where it can be hard to trace where the None value came from or how to handle it.
Re:We should be more ambitious with the software we build by saint-evan on Sunday October 25, 2026 @03:56PM (#50235471)
Well... yeah but at the cost of halting your entire program? As opposed to logging?
Re:We should be more ambitious with the software we build by wfn on Sunday October 25, 2026 @04:40PM (#50235851)
Think of it like an assert.

Sometimes you do want some flow to terminate if a schema / data contract is breached, and not have this broken data contract propagate elsewhere / outwards.

And/or, it is a perfectly valid pattern to indeed add try..catch, and contain (hence "localize") the issue there and then. If there is a way to handle this breach - you will need some kind of if check anyway. Catching by matching against the particular KeyError exception is in fact robust - and if you forgot to add an intended catch, then the exception will propagate upstack, yes - which is what you want - and this way you do not need to worry about forgetting an "if None" check.

This sort of approach makes it structurally impossible to "forget" - if exception is not caught, it will abort the cannot-be-null breach continuing with program flow, possibly (e.g.) ending up persisted in some place it shouldn't be, causing problems later.

Re:We should be more ambitious with the software we build by saint-evan on Sunday October 25, 2026 @05:55PM (#50236414)
yes, that makes sense. Thank yu so much!
Re:We should be more ambitious with the software we build by zahlman on Monday October 26, 2026 @10:52AM (#50244776)
Python uses `except`, not `catch`.
Re:We should be more ambitious with the software we build by wfn on Monday October 26, 2026 @11:33AM (#50245498)
...sure, that's true :) but you get the point. p.s. and the software design pattern applies in general, imho.