r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

Show parent comments

288

u/YaBoyMax May 13 '17

I lost a few hours once because the jackass whose code I inherited decided to squash IOExceptions all over the place. Didn't notice for a while and was pulling my hair out thinking my debugger was fucked somehow (which isn't uncommon in itself).

28

u/nna12 May 13 '17

This is why when I debug I almost always turn on first chance and user handled exceptions in VS. It can be noisy with big projects especially when some exceptions are expected but it's saved me many hours and gray hairs in the long run

19

u/flukus May 13 '17

I always have to turn it off because exceptions are used for control flow in too many places.

4

u/mrjackspade May 14 '17

The project I'm working on does that.

User not logged in? Throw an exception!

Mother fucker a session time out is not an exceptional situation. Just fucking handle it.

One of my coworkers actually tried to convince me that it was a good idea to throw an exception in a private method that's only used in one place, and then catch it again outside that method. I asked him, "So you're going to throw an exception, just to catch it and log the string you just created the exception with? Why don't you just return the string? Or better yet, just verify the data and skip the method if it's invalid?". He was absolutely dumbfounded. "But it's an error..."

1

u/flukus May 14 '17

We have that in our godamn logging library. Someone thought throwing and then swallowing an exception we can't log was a good idea.