6 ms·
Does anyone else hate multiple returns? A do/while condition would satisfy this situation. or while True: if False: break in python
by emehrkay 15y ago
Does anyone else hate multiple returns?
A do/while condition would satisfy this situation. or while True: if False: break in python
- prophetjohn 15y agoIt depends on the compactness of the code. If it's a large function, I try to avoid it, but 100% adherence to "one entry, one exit" kind of misses the spirit of the idiom. Something like: if (condition) return true; return false; is no less readable and sometimes more intuitive than the equivalent bool x = false; if (condition) x = true; return x;
- marshray 15y agoHow about this: return condition;
- ceol 15y agoI'm certain he was using True and False as arbitrary values. It's a shame his point was missed. Please try to focus on his actual argument.
- marshray 15y agoI'm not so convinced that it's not a fundamental thing. Maybe someone wants to post a 'better' example and we'll see where it goes?
- ceol 15y agoHow about: if (condition()) return 10; return 20; versus val = 20; if (condition()) val = 10; return val;
- pka 15y agoWell: val = condition() ? 20 : 10; return val;
- jonathanwallace 15y agoDid you invert the logic of the parent post on purpose?
- marshray 15y agoOr even: return condition() ? 10 : 20;
- prophetjohn 15y agoYeah, that's definitely a more reasonable way to implement the above example. I didn't put much thought into the specifics of the code, since the specifics have nothing to do with what point I was making. I was responding to a question about multiple returns; the simple example was supposed to illustrate my opinion on multiple returns, not how to return a boolean value given a boolean condition.
- perfunctory 15y agoif `if` was an expression rather than a statement one could write return if condition() 10 else 20 I am not sure this is more readable though.
- marshray 15y agoThere is an 'if' expression, it's just spelled '?' and else is ':'. Mentioned a few posts up.
- marshray 15y agoYes. 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.
- johnx123-up 15y agoI strongly hate multiple returns. OTOH, I wish python had curly braces.