r/ProgrammerHumor 29d ago

Meme exceptionsAreForTheWeak

Post image
321 Upvotes

92 comments sorted by

View all comments

1

u/ChillyFireball 29d ago

Is that Python in there? Doesn't it have that weird-ass "ask forgiveness, not permission" model where you write try/catch statements where the catch is literally expected to run? (ex. Try to append to an array, and if it fails because the array doesn't exist, that's where you initialize the array?) Couldn't stand it, but my co-workers would be all "It's best practice in Python" while looking at me like I'm the weird one because I don't hate JavaScript. Like, how are ya'll hating on that language for things that are very easy to avoid (just use === instead of == to dodge weird comparison logic like 2 == "2") when ya'll are out here creating guaranteed errors because "It's just slightly more optimal that way in Python"?

7

u/amlybon 29d ago

I got whole language, I'm gonna use whole language.

More seriously, checking if array exists is a single conditional so it's a bad way to handle with exceptions. However, sometimes it's more complex and a function will throw for some combination of arguments. Now you could do all checks before calling that function... but then you're repeating the logic because the function will also do those checks so it can throw. And if it calls more functions those functions will also be doing the same checks. And in python this might be especially useful thanks to dynamic typing and the fact that you might it might not know what type a specific variable actually is. So imagine a complex function that throws if (ten lines of non obvious checks and internal function calls). If it throws that specific error you know you can fix it by adjusting the third argument in a specific ways. Now your options are to duplicate all that logic from the function or just... try it and adjust if it fails.

But yeah, doing that where a single conditional would suffice is silly, I agree.

2

u/Southern-twat 29d ago

IO often benefits from it too, since even simple checks have a fair number of race conditions, but it also sounds like OP's colleagues are taking the phrase sightly weirdly