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

Rule proposal: return-in-conditionals #12

Open
jfmengels opened this issue Sep 15, 2016 · 2 comments
Open

Rule proposal: return-in-conditionals #12

jfmengels opened this issue Sep 15, 2016 · 2 comments

Comments

@jfmengels
Copy link
Owner

jfmengels commented Sep 15, 2016

Conditionals (in functions) that don't return a value or end the function are most likely causing a side-effect.
In the case when it is used to assign a value to a variable, in order to not have a complex ternary expression, should probably be moved to a separate function.

Switch cases should also return statement and should therefore not use break.

Top-level conditionals should be exempted from this rule, as they can't return values. (bikeshedding here?)

Invalid

function bar(a) {
  let res;
  if (expression) {
    res = 2;
  } else {
    res = 3;
  }
  return foo(res);
}

function switchFn(b) {
  switch(a) {
    case 1:
       doSomething();
       break;
    default:
       doSomethingElse();
       break;
  }
}

Valid

function bar(a) {
  return foo(value(a));
}

function value(a) {
  if (expression) {
    return 2;
  } else {
    return 3;
  }
}

function switchFn(b) {
  switch(a) {
    case 1:
       return doSomething();
    default:
       return doSomethingElse();
  }
}

// Top-level conditionals are okay
if (FOO) {
  foo();
} else {
  bar();
}
@jfmengels
Copy link
Owner Author

Maybe worth splitting the rule into two. One for if and one for switch case

@justgage
Copy link

+1

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

2 participants