question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to permit no-useless-return inside blocks

See original GitHub issue

As discussed in https://github.com/airbnb/javascript/issues/1210, it will be great if no-useless-return offers an option to allow return inside blocks, to benefit consistency between blocks when using a useless return to quit the function execution at that point.

Here is an example:

async function f(p) {
  if (p === 'a') {
    // some lines of code
    await fn()
    
    return
  }
  
  if (p === 'b') {
    // some lines of code
    await fn()
    
    return
  }

  if (p === 'c') {
    // some lines of code
    await fn()
    
    return // <-- option to allow tihs one
  }
}

Having them separated by blank lines make it clearer and easy to maintain instead of having a big if-else if-else block.

Having this last useless return enforces consistency with other blocks and also, if you add a new block below, you will not have to modify the (until now) last one block to add a return into it.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kaicataldocommented, Dec 18, 2016

@felixsanz We appreciate the suggestion regardless - thanks for contributing!

1reaction
not-an-aardvarkcommented, Dec 16, 2016

But don’t you think this is so condensed?

I don’t really see the problem myself, but you could resolve that by adding linebreaks if necessary:

  if (p === 'a') {
    await refactoredFn1()

  } else if (p === 'b') {
    await refactoredFn2()

  } else if (p === 'c') {
    await refactoredFn3()

  } else if (p === 'd') {
    await refactoredFn4()

  } else if (p === 'e') {
    await refactoredFn5()
  }

Even if refactored into functions, using useless return in the last block is useful:

But if you forget to add an explicit return statement in any of the blocks, your code will silently do the wrong thing. Personally, I think having multiple if blocks with return statements in each block is much harder to read than just using if/else directly, because to look at that code, I have to scan through each individual if statement to check if it uses return, and then I have to wonder what happens if none of the conditions match (it seems like nothing happens in your example, but it would be unclear whether this behavior is intentional).

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-useless-return breaks consistency · Issue #1210 - GitHub
On the one hand, I think no-useless-return could perhaps benefit from an option that allows a useless return inside a conditional block.
Read more >
no-useless-return - ESLint - Pluggable JavaScript Linter
A return; statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be...
Read more >
eslint-config-openstack/.eslintrc at master - OpenDev
Re-allow return, throw, break, and continue statements inside finally blocks. # http://eslint.org/docs/rules/no-unsafe-finally. no-unsafe-finally: 0.
Read more >
Disallow redundant return statements (no-useless-return)
A return; statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be...
Read more >
Documentation: 15: DECLARE - PostgreSQL
The BINARY option specifies that the cursor should return data in binary format. ... Thus, DECLARE without WITH HOLD is useless outside a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found