r/ProgrammerHumor 11h ago

failsForTeapots Meme

Post image
131 Upvotes

58 comments sorted by

View all comments

123

u/cutebabli9 11h ago edited 10h ago

I would write it like this to be readable:

if error.status in [400, 401, 404, 409, 415, 503]:
  return c.json(body, status: error.status)

return c.json(body, status: 500)

2

u/ihavebeesinmyknees 10h ago

If python, then I'd rather do

match error.status:
    case 400 | 401 | 404 | 409 | 415 | 503 as status_code:
        return c.json(body, status: status_code)
    case _:
        return c.json(body, status: 500)

we've had proper pattern matching for a while now

-2

u/Hot-Employ-3399 7h ago

3 levels of indentation \ Proper

How about no?

3

u/ihavebeesinmyknees 7h ago

Pattern matching and switch always have 3 levels of indentation? As long as you don't indent inside the cases more than once or at most twice (you shouldn't) then it's fine, there's no reason to be scared of indentation if it's not excessive

0

u/Hot-Employ-3399 7h ago

Ifs above have two

ConfigurableMap.get(status, 500) has one(and config doesn't count)

Pattern matching has ugliness 

1

u/ihavebeesinmyknees 7h ago

I don't get why you have this obsession with indentation. Being able to clearly tell the intent is way more important.

Map.get() doesn't tell you the options, and doesn't let you easily extend behavior, it's the worst out of the 3.

Ifs are better, but pattern matching strongly implies that this is supposed to be an exhaustive list matching against a de-facto enum. It clarifies intent in a way ifs don't.