javascript - Adhering to a max length setting with jshint -


I get several recommendations to compile a maximum of 80 characters when writing javascript, e.g. Google, NPM, Node. JS, Crockford In some cases, though, I do not think how best to do this. MongoClient.connect ('mongodb: // localhost: 27017 / sampleDatabase', function (mistake, database) {if (err)} {throw error;} db = database;});

This will throw a jshint warning because it exceeds 80 characters. Now, do you choose to ignore the warning in this example, or instead choose an option for a solution like

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

If you can reuse the url variable So, great option if this is a shot, as is often such a call, I would probably do something like this ...

  / * jslint sloppy: true, white: true, browser: True, maxlen: 80 * / / * global mongo client * / var dbarhh, db; DbErrHand = function (mistake, database) {if (err) {throw error; } DB = Database; // Kill me with global spaghetti! ; ^)}; MongoClient.connect ('mongodb: // localhost: 27017 / sampleDatabase', dbErrHand);  

In this way, your code is more expressive and you know which DB you are connecting to, even if Andy is just var url to Var MongoSampleDb or similar to gaining similar benefits.

I have to pull out the task, so that you can visually understand that they are rationally discrete sections, even though I know that it is not here there are more than 80 characters here if you call it Connecting to your line in the call seems that this code is also a candidate for reuse in your app.

This is also a general general habit to exclude work, so that you are not by mistake. [1]

And, of course, there is a chance that you still end with very long strings, and do something like that ...

  MongoClient.connect ('mongodb: //whoLetFredNameThisServerBecauseItsTooLong.FredsCompany.com:27017' + '/ sample database', dbErrHand); Good white spot in nested code increases this problem even more, which can +1 Andy's idea of ​​setting such variables out of any loops / IPS / nested code. At some point, it might be worth closing the  maxlen . 

But bracket handling is one of the most subjective decisions, especially in javascript, where a great, one priority answer my parameter-per-line code waving something like crazy , Or () was on its own line like this ...

  MongoClient.connect ('Mongodb: // localhost: 27017 / SampleDatabase ', dbErrHand);  

Surprisingly, JSLint still allows you to have lots of room for self expression! ^)

[1] Nepotistic Questions Link Warning, although it was the first one I googled. Probably an example of Google, for my results, um, me


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -