3 ms·
I like how the closest thing to a "correct" answer gets dismissed almost immediately with fairly weak reasoning. You're adding type hints so they're not going
by BoorishBears 4y ago
I like how the closest thing to a "correct" answer gets dismissed almost immediately with fairly weak reasoning.
You're adding type hints so they're not going to be blind sided anyways, just return a union of Item and None. If None is a valid value then use a result type.
Exceptions are for exceptional cases isn't just a performance thing, it's a reasoning thing: You're jumping out of the established control flow and there should be a very good reason for that.
If you're struggling with handling None even when you know it's a valid return value, living in a codebase that overuses exceptions is going to be 10x worse.
- moring 4y agoYou are assuming that a missing item is a normal (as in "not exceptional") case, but that is entirely application-dependent. But then, the article doesn't give any context either -- it just throws an exception in every variant of the code, so the most likely assumption here is that missing items are exceptional.
- qprofyeh 4y agoTo be honest, the example given (a "read" method that wraps direct access and raises a custom exception) does not inspire confidence that the item is expected to be there more often than not. There's certainly too little context to assume anything. Expected or unexpected, either way there are two paths; I would return a Result/Option type.
- BoorishBears 4y agoExcept the article quite literally say None is an option, except <insert not great reasons>. That wasn't an assumption on my part. If the missing item wasn't exceptional they would have just said that. - Also if even if it were exceptional, you'd still served extremely well by a Result type. Instead of just guessing where the landmine is sitting the type system can help you. Python explicitly opted out of type hints for exceptions to avoid a Java-style "checked exceptions" world.
- UncleEntity 4y ago> Exceptions are for exceptional cases isn't just a performance thing, it's a reasoning thing: You're jumping out of the established control flow and there should be a very good reason for that. Python uses exceptions for all sorts of things like to signal the end of an iteration which isn’t exceptional at all. That ship sailed a long time ago.