Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early return from a function is not always a good idea. #95

Open
baynes opened this issue Jul 14, 2017 · 2 comments
Open

Early return from a function is not always a good idea. #95

baynes opened this issue Jul 14, 2017 · 2 comments

Comments

@baynes
Copy link

baynes commented Jul 14, 2017

"always return a function's value as early as possible"

I think you should put some caveats on this. Returns (and breaks and other break out statements) buried inside blocks of code are easily missed and are a very significant source of bugs on later modifications to the code (I speak with many years of experience).

For me returns should either be in the first few lines of the function (where some simple ifs deal with trivial cases) or in the last few lines of a function.

If one has to put a return in the middle of a longer function then I would make it stand out with a comment so it is not easily overlooked.

    return;   // ****************** RETURN *********************
@tunnckoCore
Copy link

tunnckoCore commented Jul 14, 2017

Not agreed.

Returns (and breaks and other break out statements) buried inside blocks of code are easily missed and are a very significant source of bugs on later modifications to the code (I speak with many years of experience).

That's exactly the idea behind "return earlier as possible"-thing, for me. Years of experience here too, not only on node.

If one has to put a return in the middle of a longer function then I would make it stand out with a comment so it is not easily overlooked.

First, Long functions are bad :D Never need more than 50-100 (even 100 is too much for me), max 200, lines. If it's longer, that's totally clear signal that it can and must be refactored out as separate function. Second, return isn't so short to be missed, it is with other colors too.

@CommandoBM
Copy link

"den Wert einer Funktion immer so früh wie möglich zurückgeben"

Ich denke, Sie sollten diesbezüglich einige Vorbehalte machen. Rückgaben (und Breaks und andere Breakout-Anweisungen), die in Codeblöcken versteckt sind, werden leicht übersehen und sind eine sehr bedeutende Quelle von Fehlern bei späteren Modifikationen des Codes (ich spreche mit langjähriger Erfahrung).

Für mich sollten Rückgaben entweder in den ersten Zeilen der Funktion (wo einige einfache ifs triviale Fälle behandeln) oder in den letzten Zeilen einer Funktion stehen.

Wenn man einen Return mitten in eine längere Funktion setzen muss, dann würde ich ihn mit einem Kommentar hervorheben, damit er nicht so leicht übersehen wird.

    return;   // ****************** RETURN *********************

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants