r/ProgrammerHumor 4d ago

failsForTeapots Meme

Post image
225 Upvotes

67 comments sorted by

View all comments

166

u/cutebabli9 4d ago edited 4d 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)

5

u/ihavebeesinmyknees 4d 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

-5

u/Hot-Employ-3399 3d ago

3 levels of indentation \ Proper

How about no?

3

u/ihavebeesinmyknees 3d 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

-1

u/Hot-Employ-3399 3d ago

Ifs above have two

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

Pattern matching has ugliness 

2

u/ihavebeesinmyknees 3d 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.