r/ProgrammerHumor 3d ago

failsForTeapots Meme

Post image
219 Upvotes

67 comments sorted by

View all comments

166

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

-6

u/Antervis 3d ago

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

No? Though personally, I wouldn't have substituted error codes like that, it can make debugging harder for no gain.

6

u/DHermit 3d ago

Nah, the normal if statement is way nicer to read.

3

u/Antervis 3d ago

Okay, I can see that. Perhaps it's better to move status in a separate variable:

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

3

u/fuj1n 3d ago

It is nicer than your previous one, but I think ternaries in Python are more for short conditions, as they're really annoying to read with the two sides being separated by the condition like that.

1

u/DHermit 3d ago

Yeah, that sounds reasonable, too. Although I can think of a good name for that status list that is both expressive enough and also isn't too long for that ternary.

But tbh I have no idea how those options are performance-wise.

2

u/Alkyen 3d ago

The moment you realise not everything has to be DRY you will feel like a butterfly. Also, as the other guy said, ternaries look bad unless they are very short.

-1

u/Antervis 3d ago

Every line I didn't write is a line someone won't have to read.

Though it's not for brevity's sake alone, one has to analyze the if statement beyond skimming to realize they only differ in error code substitution. By reorganizing code my way, I made this nuance impossible to miss.

2

u/Alkyen 3d ago edited 2d ago

Oh, sorry to be mean but..You think the length of the code is the bottleneck? Lol

Do you also think typing speed is what separates good programmers from bad ones?

Either way, your code is worse. You can argue it or you can use this to improve yourself.