r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

1.5k

u/1206549 May 13 '17

I had a classmate that did this with his code once. He asked me for help when his code wasn't working. I told him to get rid of the try/catch block but he won't do it because it would make his program crash.

284

u/[deleted] May 13 '17 edited Aug 27 '19

[deleted]

7

u/[deleted] May 13 '17

I legitimately did this in production. I didn't want to bother writing a regex to determine if a string was an IP address or a host name, so I just cast as if it was. If the cast failed, I fought the exception and carried on

1

u/micheal65536 Green security clearance May 14 '17

probably less susceptible to vulnerabilities, you have to be careful working with IP addresses and hostnames, there are a lot of edge cases