Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes. I generally try to avoid them in my code, unless doing so makes the logic significantly more contorted and then I put a prominent comment way out on the right margin to draw attention to the inner return.

That first get_cached_user example probably doesn't need to be nested. I don't know about Python but I would expect an optimizing compiler to short circuit the redundant conditionals if it really mattered. If it really did matter for performance, the aesthetics become a distant second consideration.

I don't see anything wrong with the get_media_details example, other than perhaps the logic of the example itself. It looks readable to me, but a blank line after 9, 13, and 19 would help with readability. If it were to grow more complicated I might look at refactoring using some sort of object polymorphism.

To me the only thing worse than complex deeply-nested control structures is code that tries to hide that complexity for aesthetic reasons in control structures that are only superficially simpler.



Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: