max-lines-per-function
在函数中强制执行最大代码行数
有些人认为大型函数是一种代码味道。大型函数往往会做很多事情,并且很难跟踪正在发生的事情。许多编码风格指南规定了函数可以包含的行数限制。这条规则可以帮助强制执行这种风格。
¥Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard following what’s going on. Many coding style guides dictate a limit of the number of lines that a function can comprise of. This rule can help enforce that style.
规则详情
¥Rule Details
此规则强制每个函数的最大行数,以帮助可维护性并降低复杂性。
¥This rule enforces a maximum number of lines per function, in order to aid in maintainability and reduce complexity.
为什么不使用 max-statements
或其他复杂度测量规则呢?
¥Why not use max-statements
or other complexity measurement rules instead?
为了便于阅读,像下面的示例这样的嵌套长方法链通常被分成单独的行:
¥Nested long method chains like the below example are often broken onto separate lines for readability:
function() {
return m("div", [
m("table", {className: "table table-striped latest-data"}, [
m("tbody",
data.map(function(db) {
return m("tr", {key: db.dbname}, [
m("td", {className: "dbname"}, db.dbname),
m("td", {className: "query-count"}, [
m("span", {className: db.lastSample.countClassName}, db.lastSample.nbQueries)
])
])
})
)
])
])
}
-
尽管有 16 行代码,
max-statements
只会将此报告为 1 条语句。¥
max-statements
will only report this as 1 statement, despite being 16 lines of code. -
complexity
只会报告复杂度为 1¥
complexity
will only report a complexity of 1 -
max-nested-callbacks
只会报 1¥
max-nested-callbacks
will only report 1 -
max-depth
将报告深度为 0¥
max-depth
will report a depth of 0
选项
¥Options
此规则具有以下可使用对象指定的选项:
¥This rule has the following options that can be specified using an object:
-
"max"
(默认50
)强制函数中的最大行数。¥
"max"
(default50
) enforces a maximum number of lines in a function. -
"skipBlankLines"
(默认false
)忽略纯粹由空格组成的行。¥
"skipBlankLines"
(defaultfalse
) ignore lines made up purely of whitespace. -
"skipComments"
(默认false
)忽略仅包含注释的行。¥
"skipComments"
(defaultfalse
) ignore lines containing just comments. -
"IIFEs"
(默认false
)包括 IIFE 中包含的任何代码。¥
"IIFEs"
(defaultfalse
) include any code included in IIFEs.
或者,你可以为 max
选项指定一个整数:
¥Alternatively, you may specify a single integer for the max
option:
"max-lines-per-function": ["error", 20]
相当于
¥is equivalent to
"max-lines-per-function": ["error", { "max": 20 }]
code
具有特定最大值的此规则的错误代码示例:
¥Examples of incorrect code for this rule with a particular max value:
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
var x = 0;
}
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
// a comment
var x = 0;
}
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
// a comment followed by a blank line
var x = 0;
}
具有特定最大值的此规则的正确代码示例:
¥Examples of correct code for this rule with a particular max value:
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
var x = 0;
}
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
// a comment
var x = 0;
}
/*eslint max-lines-per-function: ["error", 5]*/
function foo() {
// a comment followed by a blank line
var x = 0;
}
skipBlankLines
使用 { "skipBlankLines": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "skipBlankLines": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/
function foo() {
var x = 0;
}
使用 { "skipBlankLines": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "skipBlankLines": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/
function foo() {
var x = 0;
}
skipComments
使用 { "skipComments": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "skipComments": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 2, "skipComments": true}]*/
function foo() {
// a comment
var x = 0;
}
使用 { "skipComments": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "skipComments": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 3, "skipComments": true}]*/
function foo() {
// a comment
var x = 0;
}
IIFEs
使用 { "IIFEs": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "IIFEs": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/
(function(){
var x = 0;
}());
(() => {
var x = 0;
})();
使用 { "IIFEs": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "IIFEs": true }
option:
/*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/
(function(){
var x = 0;
}());
(() => {
var x = 0;
})();
何时不使用
¥When Not To Use It
如果你不关心函数中的行数,可以关闭此规则。
¥You can turn this rule off if you are not concerned with the number of lines in your functions.
相关规则
版本
此规则是在 ESLint v5.0.0 中引入。