-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
return-early
rule
#16
Comments
Like that style as well! Would be nice. |
👍 for the idea. I found this recently https://github.com/Shopify/javascript/blob/master/packages%2Feslint-plugin-shopify%2Fdocs%2Frules%2Fprefer-early-return.md, which handles a related case. I would also handle the following: // handled by the above mentioned plugin
function foo() {
if (condition) {
...
}
// nothing else
} but not function foo() {
let value;
if (condition) {
value = X;
} else if (condition2) {
value = Y;
} else {
value = Z;
}
// do something with value like
return foo(value);
// return value or anything else
}
// the alternative would be
function foo() {
let value;
if (condition) {
return foo(X);
} else if (condition2) {
return foo(Y);
}
return foo(Z);
}
// which is longer to refactor |
I think I've already been bitten by http://eslint.org/docs/rules/no-negated-condition, that made it not possible to return early, so in XO you may have to relax the rule a bit (or create a new rule that handles both cases?). |
|
True, but it does in the case of my first example, which I think would be nice to also handle in this plugin (though it doesn't have to as there already exists a rule for it). |
I disagree with the rule as proposed. I do agree with the sentiments from @timoxley's blogpost. But those are different from the code being discussed in sindresorhus/execa#15 (diff). In Tim's blog he recommends against this: function (err, result) {
if (err) {
handleError(err);
} else {
processTheResult(result);
processTheResultMore(result);
stillMore();
// ...
}
} In that case I agree with the refactor to use a return statement. You are saying: "If there is an error,handle it, but ignore the rest of this - the remaining function is moot because there is an error". Also, for every line you get to un-indent in that Up until this point, I'm in agreement with everyone I think. Where I disagree is the simple function (input) {
if (isStream(input)) {
handleStream(input);
} else {
handleString(input);
}
} In this case you're not bailing (returning) early because there is an error. There are two branches your code could take, and that is clearly reflected with the explicit IMO - |
This comment has been minimized.
This comment has been minimized.
This could/should also apply to Incorrectfor (x in y)
if (isGreen(x))
if (isBold(x))
if (isItalics(x))
console.log('All good') Correctfor (x in y) {
if (!isGreen(x))
continue
if (!isBold(x))
continue
if (!isItalics(x))
continue
console.log('All good')
} |
There are similar rules for PHP code (https://github.com/slevomat/coding-standard#slevomatcodingstandardcontrolstructuresearlyexit-) and I think it would make a lot of sense here too, especially to avoid nested tons of nested ifs. Generally: (what I came up with when checking some code samples) Logic:
if ( foo )
if ( bar ) => give error for this
if ( foo )
if ( bar )
if ( qwe ) => give error for this
elseif ( xyz )
if ( abc )
if ( bla ) => give error for this |
Any update on this topic? Do anyone know is there a rule like that implemented somewhere? |
@thigrand might answer your question: https://github.com/Shopify/web-configs/blob/main/packages/eslint-plugin/lib/rules/prefer-early-return.js. |
@belgattitude (also further up in the thread @jfmengels) nice find! Some additional details:
|
I tried that plugin. I think this should be allowed: function bar() {
if (foo) {
onlyStatementHere()
}
} It does "unnecessarily" increase indentation, but I think changing it to this is kind of annoying and not any more readable: function bar() {
if (!foo) {
return;
}
onlyStatementHere()
} However in this case it starts to tip the scale a bit: function bar() {
if (foo) {
many()
other.statements = 1;
if (here) {
log(lotsOfLines)
}
how = 'many?'
}
} Maybe optionally it can allow it as long as there are no further statements outside the condition.
function bar() {
if (foo) {
many()
other.statements = 1;
if (here) {
log(lotsOfLines)
}
how = 'many?'
+ return;
}
+ return 'nah'
} |
Just set |
As discussed in sindresorhus/execa#15 (diff). How I think when coding is to handle errors and exceptions as soon as possible and return early. If we take a look at the two examples below, the latter one makes more sense to me.
foobar()
in this case is the "real" body while the code in theif
statements deal with exceptions. When using the style in the former one, this isn't as clear to me.This might be worth reading too http://blog.timoxley.com/post/47041269194/avoid-else-return-early.
The text was updated successfully, but these errors were encountered: