Follow

While I appreciate the advantages of "Easier to Ask for Permission than Forgiveness", given how poorly distinguished exception type hierarchies are in , for the sake of providing good error reporting, I find "Look Before You Leap" makes a lot of sense a lot of the time. I'm thinking of the cases where you have a set of options for sets of provided parameters: Checking what's intended and then doing just that thing means that when the operation fails, I can report "<commanded operation> failed" vs trying each thing in series as the previous thing fails and then reporting all of the things that might have failed, only one of which was the intended one.

Really, I haven't found much reason to prefer EAFP outside of certain situations like dictionary accesses, where "checking" is the same thing as access, so it's redundant, or where checking first would cause TOCTOU (en.wikipedia.org/wiki/Time-of-) errors, in which case it's just a correctness thing. Maybe my example above for error reporting is a strawman and not what people have in mind when they say you should prefer EAFP, but given that it looks like basically the same kind of case where EAFP *is* generally recommended (until it doesn't), seems like without talking about the logic behind it (TOCTOU, redundancy, maybe others), you'll tend to mispredict the right approach.

@2ck there's this common misconception you have to choose one or the other. In my experience it's a case-by-case decision mostly driven by the design of the API one's consuming.

OTOH when designing your own API you can be as granular in your exception as needed, so the question is moot :-)

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.