5 ms·
> I've been taught by some pretty experienced engineers that in terms of readability, multiple return statements are a bad idea They were wrong and probably no
by mseepgood 14y ago
> I've been taught by some pretty experienced engineers that in terms of readability, multiple return statements are a bad idea
They were wrong and probably not as experienced as they/you thought. Guard clauses make code simpler and more understandable.
- adamnemecek 14y agoGuard clauses and single return statements are not mutually exclusive.
- masklinn 14y agoThe whole idea of guard clauses in algol-derived languages (not functional ones) is to bail out immediately with an early return... http://martinfowler.com/refactoring/catalog/replaceNestedConditionalWithGuardClauses.html http://martinfowler.com/refactoring/catalog/replaceNestedCon...
- adamnemecek 14y agoI understand that but you can have a macro wrapping a goto statement to a predefined label which will do that for you (and potentially set some errors). It's debatable whether this really gives you anything but I kind of like this style since you can replace the whole if statement with a single line something like check_memory(pointer);. The goto's become particularly useful if you want to have some cleanup done at the end of the function even if something during the function fails.
- masklinn 14y agoNow your assuming the language has goto. Which definitely is not the case these days.
- adamnemecek 14y agoWell the discussion is about go and go does have gotos. http://golang.org/ref/spec#Goto_statements http://golang.org/ref/spec#Goto_statements