4 ms·
I’ve worked with people who stick to “only one return statement per function”. Super annoying.
by dls2016 4y ago
I’ve worked with people who stick to “only one return statement per function”. Super annoying.
- cebert 4y agoI think it depends on the situation, but a single return statement per method does make it a bit easier to read a method and understand flow control.
- Tyr42 4y agoReally exit can be pretty useful though. Doing if(condition) return at the start, before it gets going often reduces indentation and how many branches you need to see at once. But I can agree sneaky returns can bite you sometimes
- HWR_14 4y agoAt the top of the function, "if [special input] return [special output]" is a great way of handling edge cases
- dhzhzjsbevs 4y agoThe no else statement shit drives me up the wall. Sometimes code is more readable when it says what you mean. Sometimes I want to branch, other times I want to return early. There is a difference in what I mean and code should reflect that even if the opcode is the same.
- StellarScience 4y agoWe've adopted single entry / single exit except for short functions (< 10-15 lines), where all the returns are clearly visible at once. It seemed to placate both sides, and it allows you to write that simple 'findFoo' function that returns 'foo' as soon as it finds it.
- zasdffaa 4y agoI'm one of those and rather unsure of its value. The original reason is the modelling of the program as a reducible graph (if I'm not getting confused with other stuff). I occasionally have a couple of returns in one func but only for very, very simple and short funcs where it's clearer. I dunno. Perhaps the problem is we're missing some higher level procedural construct - any ideas?