2

I see several recommendations for adhering to an 80 character max line length when writing javascript, e.g. 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. 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;
     }
 );
Philip O'Brien
  • 4,146
  • 10
  • 46
  • 96
  • Most decent text editors have an option to draw a line on the page at the specified number of characters (it's called different things in each editor). In Notepad++ its under Setting | Editing | Vertical Edge Settings. –  Oct 17 '14 at 10:01
  • In sublime text you add `"rulers": [80, 100, 120]` to your user prefs. SublimeText also has great jshint plugins (sublimelinter for example). – Andy Oct 17 '14 at 10:06
  • Are those rulers not just visual indications to remind you not to run over them? Or do they do something behind-the-scenes? – Philip O'Brien Oct 17 '14 at 10:45
  • No, just visual cues. – Andy Oct 17 '14 at 10:50
  • Ah ok, that's not what I'm looking for. JSHint is giving me my cues whenever I run it. I was looking to see how people actually avoid overly long lines. – Philip O'Brien Oct 17 '14 at 10:53

2 Answers2

2

If you can reuse the url variable, Andy's is a great option. 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.

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. 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]

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. At some point, it might be worth turning maxlen off.

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. 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! ;^)

[1] Nepotistic question link alert, though it was the first one I googled up. Probably an example of Google biasing my results for, um, me.

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138
1

I would separate out the url into a new variable.

var url = 'mongodb://localhost:27017/sampleDatabase';
MongoClient.connect(url, function(err, database) {
  if(err) {
    throw err;
  }
  db = database;
});
Andy
  • 61,948
  • 13
  • 68
  • 95