简体   繁体   English

使用jshint遵守最大长度设置

[英]Adhering to a max length setting with jshint

I see several recommendations for adhering to an 80 character max line length when writing javascript, eg Google, npm, Node.js, Crockford. 在编写javascript时,我看到一些建议坚持最大80个字符的行长,例如Google,npm,Node.js,Crockford。 In certain cases, however, I don't see how best to do it. 但是,在某些情况下,我看不出如何做到最好。 Take the example below 请看下面的例子

MongoClient.connect('mongodb://localhost:27017/sampleDatabase', function(err, database) {
  if(err) {
    throw err;
  }
  db = database;
});

That would throw a jshint warning since it exceeds 80 characters. 这将引发jshint警告,因为它超过80个字符。 Now, would you choose to ignore the warning in this instance, or instead opt for a solution such as 现在,您是选择忽略这种情况下的警告,还是选择解决方案,例如

MongoClient.connect('mongodb://localhost:27017/sampleDatabase', 
     function(err, database) {
       if(err) {
         throw err;
       }
      db = database;
     }
 );

If you can reuse the url variable, Andy's is a great option. 如果您可以重用url变量,那么Andy's是一个不错的选择。 If it's a one shot, as calls like this often are, I'd probably do something like this... 如果是一个镜头,就像经常这样的通话,我可能会做这样的事情...

/*jslint sloppy:true, white:true, browser: true, maxlen:80 */
/*global MongoClient */

var dbErrHand, db;
dbErrHand = function(err, database) {
    if(err) {
        throw err;
    }
    db = database;  // Killing me with the global spaghetti!  ;^)
};

MongoClient.connect(
    'mongodb://localhost:27017/sampleDatabase', 
    dbErrHand
);

That way, your code is more expressive and you know what db you're connecting to, though Andy just needs to change var url to var mongoSampleDb or similar to get the same advantage. 这样,您的代码更具表现力,并且您知道要连接到的数据库,尽管Andy只需将var url更改为var mongoSampleDb或类似内容即可获得相同的优势。

I like to pull the functions out so you can visually understand that they're reasonably discrete pieces of logic, even though I realize it isn't over 80 chars here if you put it on its own lines in the connect call. 我喜欢拉出这些函数,以便您可以直观地理解它们是逻辑上离散的部分,即使我意识到如果将其放在connect调用中的单独行中,此处的字符数也不超过80个。 Would think that code is a candidate for reuse in your app as well. 也会认为代码也是在您的应用程序中重用的候选者。

It's also a good general habit to pull out functions so you don't accidentally make a function inside of a loop . 拔出函数也是一个很好的一般习惯,这样就不会在循环内意外创建函数 [1] [1]

And, of course, there's a chance that you still end up with insanely long strings, and have to do something like... 而且,当然,您仍有机会以疯狂的长字符串结尾,并且必须执行类似的操作……

MongoClient.connect(
    'mongodb://whoLetFredNameThisServerBecauseItsTooLong.FredsCompany.com:27017'
        + '/sampleDatabase', 
    dbErrHand
);

Good whitespace in nested code exacerbates the problem even more, which might +1 to Andy's idea of setting up variables like this outside of any loops/ifs/nested code. 嵌套代码中良好的空格会进一步加剧该问题,这可能会使Andy在任何循环/ ifs /嵌套代码之外设置此类变量的想法+1。 At some point, it might be worth turning maxlen off. 在某个时候,关闭maxlen可能值得。

But bracket handling is one of the most subjective decisions there is, especially in JavaScript, where there's no sniff of a great, a priori answer. 但是括号处理是最主观的决定之一,尤其是在JavaScript中,没有嗅到一个很好的先验答案。 Some bristle like crazy at my parameter-per-line code, or would prefer the ( was on its own line, like this... 有些鬃毛喜欢我的每行参数代码,或者更喜欢(在自己的行上,像这样...

MongoClient.connect
(
    'mongodb://localhost:27017/sampleDatabase', 
    dbErrHand
);

Surprisingly, JSLint still allows you plenty of room for self-expression! 令人惊讶的是,JSLint仍然为您提供了足够的自我表达空间! ;^) ; ^)

[1] Nepotistic question link alert, though it was the first one I googled up. [1] 裙带关系的问题链接警报,尽管它是我谷歌搜索的第一个。 Probably an example of Google biasing my results for, um, me. 可能是Google偏向我(我)的结果的一个例子。

I would separate out the url into a new variable. 我会将URL分离到一个新变量中。

var url = 'mongodb://localhost:27017/sampleDatabase';
MongoClient.connect(url, function(err, database) {
  if(err) {
    throw err;
  }
  db = database;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM