max-lines-per-function

在函数中强制执行最大数量的代码

有些人认为大型函数是一种代码味道。大型函数往往会做很多事情,并且很难跟踪正在发生的事情。许多编码风格指南规定了函数可以包含的行数限制。这条规则可以帮助强制执行这种风格。

规则详情

此规则强制每个函数的最大行数,以帮助可维护性并降低复杂性。

为什么不使用 max-statements 或其他复杂度测量规则呢?

为了便于阅读,像下面的示例这样的嵌套长方法链通常被分成单独的行:

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 条语句。
  • complexity 只会报告复杂度为 1
  • max-nested-callbacks只会报1
  • max-depth 将报告深度为 0

选项

7EcQuCaIWYQLimLMKKSRkbM1KakqNAKeQHxnCAgIp1K+gy7nTqL47XwVQQ7u/DaRlqakeqyjhPiKhLJjiNpAhg==

    yRgE+lukeoid7f1uhgPRB39254q1TGbeJuJUZeGCLk6uBSOpmtr/l5MypEDURIEPB0WPB3UDu0Xb+rEaFQaM1CjIZruhJZA0WpKbaCujHUMRjAd3JHfLVMC7ENqUUyZKkB6ClzhSNxRtylwBUVR42Ds4Rn/cUaLcV07/qL9ZlvHdGsOKUBA/hd5Hi7mTJDRYAGzmP2gxOTySg0sC3uvlozeU8UHMg+fGMDtlGrFeonUXXdlkbHPGAoOl7yaexJtp1bXMvkZfJk5GjQQfA5lpTjApG+SC3dNwSwD5YXLCzZDocSdlnR3stMMIpSMKmFopqhXydAs2ioo2iCDg05xrGlWAg9rc3en/AfC2Y3j6KU62WNJfHBxgJX49qqHn5/vcgnU7/dSdhg2YV/6Vgksbeh1Eoxb6whosBK3o3matDIKPdQduLbtlsWnpDergTP3cDK2l4DkjYrdGDaiuYuya/tkgUj647ePCTpDRwoZw9Hh/tQ4MhR8muoBtokukxX1LSJAdgaGWfgXl3pf3mj2RWObhTyMQ+8IdZD2K3bbiMgfZELSLhrI0GoUGPwja7vh8jsIPF25eJF1DyGzAQIG3kQ==

e97xBIF8d/OGzpD25hMIWlm/jOGmhtGEOeU+D5+pqXIxWKkly6ZzaxzrnxFK6Is4XtLFl9KHXEiY57ar90gOrOShFwE6Hou4UHEKxlkHFcI=

"max-lines-per-function": ["error", 20]

9EuVjgNjWLa0FpEQ/u1P/A==

"max-lines-per-function": ["error", { "max": 20 }]

code

53qbJC6xNcTpfWzDOxH3FbxQniXPcALMzc0838bz9byjjcSuhV7Vu+2FZZBAlZTPkSgXv2dJOT6yjbFgc7eBoQ==

/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    // a comment
    var x = 0;
}
/*eslint max-lines-per-function: ["error", 2]*/
function foo() {
    // a comment followed by a blank line

    var x = 0;
}

x8vS/D2xVZSiiHLs1PwUl6/TVEXV0uticb8dGcMm9rzhg2C1ABwD/0ADF6B/1bjyj8FJ90WnzVndOAKS+XgjYg==

/*eslint max-lines-per-function: ["error", 3]*/
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", 3]*/
function foo() {
    // a comment followed by a blank line

    var x = 0;
}

skipBlankLines

5nEv4RCYCaBiSjFxJ4F/mHu0c2ij5sLZlgPwr0rp5iAP+r+YRtI15XgXhkIbwWOGy4FFzLvtCcwkFw9eMApymCapHBK83jIZ4yWtiIcu+EnQ6rAD6oKwgl1T/rC2cR+M

/*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/
function foo() {

    var x = 0;
}

5nEv4RCYCaBiSjFxJ4F/mHu0c2ij5sLZlgPwr0rp5iAP+r+YRtI15XgXhkIbwWOGy4FFzLvtCcwkFw9eMApymIsAKaZ1pcl9hrkXYD0OeV9BwpyBGdED9z/0wy/5lRwd

/*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/
function foo() {

    var x = 0;
}

skipComments

5nEv4RCYCaBiSjFxJ4F/mIRmiUC1TwUHP1SzJjdrgB645dbyoMQJvcLtE595ZgEz1Wv+RRKQIpgwmffISISYmKf1pAVZBsJ3WaG+ze57E/ta6NLqtvWD3tCMuzJgk2UU

/*eslint max-lines-per-function: ["error", {"max": 2, "skipComments": true}]*/
function foo() {
    // a comment
    var x = 0;
}

5nEv4RCYCaBiSjFxJ4F/mIRmiUC1TwUHP1SzJjdrgB645dbyoMQJvcLtE595ZgEzivueF6S01kwxdxHEfgtUY7Urdhex8Fs2lQ68dJTuHS5KvxM0u9zteL/O6xqGr1ps

/*eslint max-lines-per-function: ["error", {"max": 3, "skipComments": true}]*/
function foo() {
    // a comment
    var x = 0;
}

IIFEs

5nEv4RCYCaBiSjFxJ4F/mIoES2YdisTNQBCAcsm/B9urT3pNRzRvGq/u84KwFo1V/MOUlTV8uAMO0Oz1+EMmvmiuFhSh05384l/olWkp2ww=

/*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/
(function(){
    var x = 0;
}());

(() => {
    var x = 0;
})();

5nEv4RCYCaBiSjFxJ4F/mIoES2YdisTNQBCAcsm/B9urT3pNRzRvGq/u84KwFo1VP53sgy1yhNNVR01tegzaCz1vSNuR54A354ZndKFsrYw=

/*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/
(function(){
    var x = 0;
}());

(() => {
    var x = 0;
})();

何时不使用

JXxXmrUH/onQ9XxSSR0VF5vSZSm0Axg1KD25QuQ727VlGWLxMOBI0csh5lhG3FaypNOVSEHdxRCx7WQ2nexs6A==