开发者

jslint - Should we tolerate misordered definitions?

开发者 https://www.devze.com 2023-03-21 01:57 出处:网络
Update: Turns out that this is a duplicate of: JSLint: Using a function before it\'s defined error We have freshly adopted jslint in our project, and as it promised - jslint starts hurting our feelin

Update: Turns out that this is a duplicate of: JSLint: Using a function before it's defined error

We have freshly adopted jslint in our project, and as it promised - jslint starts hurting our feelings. But not only that, it makes us questioning quite a few of the rules it has. To be honest, to me it resembles ANSI C programming style somehow. Here's one:

JS Lint: 'someMethodFoo' was used before it was defined.

This occurs because we use a top-down coding style: Each file first contains the most important method. The implementation of that method is broken down into "sub"-method calls to keep the code clean and well-structured. This implies that in most cases, methods are used before they are defined. In fact, I consider this a good practice, it makes the code better understandable for future readers, and thus more maintainable.

Why would JS Lint want us to turn it upside down?

We could simply turn off this option by using 开发者_如何转开发the --undef flag. But are there reasons against that? For example, does the order influence the performance of an interpreter? Does that matter in an age when every browser sooner or later switches from interpreters to compilers?


It's just a convention and the code conventions page doesn't give any explanation as to why it's enforced that way

All functions should be declared before they are used. Inner functions should follow the var statement. This helps make it clear what variables are included in its scope.

We could read it as:

All functions should be declared before they are used. This helps make it clear what functions are available and what functionality they provide.

I think that it's recommended in order to give somebody reading the code a chance to understand what a function does before using it. If you prefer a top-down approach, i think that's totally acceptable and you should use the --undef flag to reflect your conventions. After all, the main goal of the tool is to help you :)

edit: this question on SO may help clarify why it's useful as a warning

0

精彩评论

暂无评论...
验证码 换一张
取 消