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

> 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.



Guard clauses and single return statements are not mutually exclusive.


The 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/replaceNestedCon...


I 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.


Now your assuming the language has goto. Which definitely is not the case these days.


Well the discussion is about go and go does have gotos. http://golang.org/ref/spec#Goto_statements




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

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

Search: