Your code example also has an alias - the Christmas tree, and in real projects it is extremely difficult to maintain such a code. For example, you need to add another condition - you put another if block inside an existing one, etc. etc., and it will be a nightmare ... and as a result you will have an awfull tree ...
Alternatively, you can do something like this:
if (err) { if (err.message) { return err.message; } if (err.errorCode) { return err.errorCode; } } return "Unknown error.";
Such code looks simpler, right?
So, I really think that for such purposes it makes sense to use return .
BUT, I think the main point here is not to lose consistency ! In our example, we always return the result to the same data type and with the same business logic and, for example:
if (err) { if (err.message) { return err; } // Here some stuff... // Few lines of code... // return -1; } // Another code... // if (!user) { return null; } // And somewhere in the end on anothere scrollin screen // return user.email;
this example returns object or number or null or string - and this is another nightmare ...
And in real projects in large functions, it is really easy to get such a code due to the return of the batch ...
So, itβs better to have fewer returns, again because itβs easy to maintain ...
source share