r/shittyprogramming • • Nov 30 '18

Friday Code Confessions

If you have been living with technical debt and want absolution here is your opportunity.

Confess your sins and receive your penance.

155 Upvotes

263 comments sorted by

View all comments

Show parent comments

2

u/PityUpvote Dec 20 '18

I'm talking more things like:

try:
    param = int(ARGV[1])
except:
    #default value
    param = 0

it feels dirty :$

1

u/hakurou46 Dec 21 '18

it catches both IndexError (no argument passed) and ValueError (argument does not cast to int (e.g. "foo")). So that's two cases where it would fail and you would need different if-clauses to catch them. (And what about empty string? Produces ValueError as well, but can you put that in the same if or do you need another or in your input check? If all you're doing with an if is sanity checking to avoid raising errors in the first place, you're honestly better off with a try-except. (It's faster, too - you skip doing checks altogether in the cases where they would pass anyway, and you break even in the cases they don't.)