You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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.
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.
"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.
"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.
The text was updated successfully, but these errors were encountered: