jslint - Should we tolerate misordered definitions?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:17:41

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/6744692/jslint-should-we-tolerate-misordered-definitions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!