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.

`no-use-before-define` with arrow functions and the top-to-bottom/the stepdown rule Clean Code principle.

See original GitHub issue

What rule do you want to change? no-use-before-define

The no-use-before-define makes it so you cannot follow the ‘top-to-bottom’ principle or ‘the stepdown rule’ from the book Clean Code (by Robert C. Martin or Uncle Bob if you will) when declaring arrow functions. You can get around this by sticking with using the function keyword, but I (and I assume others) would like to use arrow functions instead because of the different behavior of this.

I think this change behavior is important because it’s nice to be able to have an abstract function at the top of your file that explains the behavior of the rest of the file. This function can call other functions that abstract uninteresting details.

Does this change cause the rule to produce more or fewer warnings? Fewer

How will the change be implemented? (New option, new default behavior, etc.)? I have a couple suggestions.

  1. Add a new option arrow-function that checks for arrow functions after let and const
  2. Added behavior: Check if lowerLevelFunction is called inside another function (higherLevelFunction) and only throw an error if higherLevelFunction is invoked before lowerLevelFunction is declared. This would need to be checked multiple levels deep of course, but I’m sure you get the point.

Please provide some example code that this change will affect:

Before:

// .eslintrc.js
"no-use-before-define": ["error", { 
      "functions": false,
      "classes": true,
      "variables": true }]

// app.js
const higherLevelFunction = () => {
       // stuff

       const abstractedStuff = lowerLevelFunction(); // no error

       // more stuff
};
const lowerLevelFunction = () => {
       // do something
      return something
};

After

// .eslintrc.js
"no-use-before-define": ["error", { 
      "functions": true,
      "classes": true,
      "variables": true,
      "arrow-functions": false }]

// app.js
const higherLevelFunction = () => {
       // stuff

       const abstractedStuff = lowerLevelFunction(); // no error

       // more stuff
};
const lowerLevelFunction = () => {
       // do something
      return something
};

What does the rule currently do for this code? Throw errors about no-use-before-define when it should not happen

What will the rule do after it’s changed? Consider arrow functions as an exception or check the order of function calls like I explained earlier.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kaicataldocommented, Jul 10, 2018

@ljharb Thanks for sharing your thoughts! For what it’s worth, I agree with you.

If we were to add this change, I do think it should include non-arrow function expressions as well. That being said, I’m not entirely convinced this meets our bar of being absolutely necessary. It also feels like it could make things confusing if we not all variables are covered under the variables option.

0reactions
eslint-deprecated[bot]commented, Dec 11, 2018

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Stepdown Rule - DZone
The stepdown rule dictates that code in a class should be readable from top to bottom, descending levels of abstraction as you go....
Read more >
The step-down rule - Marek Hudyma's
The step-down rule is one of my favorite rules presented by Uncle Bob in his book “Clean Code: A Handbook of Agile Software...
Read more >
The Stepdown Rule in Clean Code - Stack Overflow
What should I do when I use coffeescript since there is no function declarations in coffeescript. example: seeAMovie = ()-> BuyTheTicket() watch ...
Read more >
Dart Arrow Function - YouTube
Clean code writing tips using arrow function. We will see how to write arrow function for PHP, Java, Dart and JavascriptTwitter @dbestechBuy ...
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