In my project (browser context only) I want to use a JS code quality tool. I've tried both jslint and eslint. I want linter to help me make my code clean, clear, errorproof and improve its overall quality. What I don't want to do is I don't want to write some dirty hacks or use bad practices just to make linters happy.
I'm concerned about only one issue. Both of them reported a problem that I'm using a function before it was defined. Obviously in the following code snippet bar won't be called before it's definition.
function foo() {
bar();
}
function bar() {
}
foo();
In this simplest scenario I can just move bar before foo. But there are cases when it's just impossible. First function uses the second, the second uses the third and the third uses the first.
It seems like I can make linters happy by declaring all functions before their definitions like this.
var foo;
var bar;
foo = function() {
bar();
};
bar = function() {
};
foo();
The questions are:
- Is the first code snippet broken? I guess - not.
- Is the first code snippet error-prone? I guess - maybe.
- Is it a good practice to organize code like the second snippet (declare functions before defining them)?
- If
yesI should stick to this practice, shouldn't I? - If
nowhat is the good practice regarding this issue? - Is this linter error worth paying attention to or should I just disable it?
fooandbarin your code?