Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding Style Guide for node.js apps? [closed]

Is there a (or several) coding style guide for node.js? If not, what are the emerging styles used by the top open-source node projects?

I'm looking for a guide (or several guides) along the lines of PEP 8, the canonical Coding Style Guide for Python. I've seen various JavaScript guides not worth linking here (mostly old and targeted at client-side JavaScript). I found one interesting node.js style guide.

A coding style guide, or coding conventions, should include (but is not limited to):

  • Code layout: indentation (2 spaces, 4 spaces, tabs, ...), newlines, line breaks, etc.
  • Whitespace, e.g., "function (arg)" vs. "function(arg)"
  • Semicolon or no semicolon, var declaration, ...
  • Naming, e.g., do_this() vs. doThis(), var_name vs. varName, ...
  • node.js and JavaScript idioms, e.g., == vs. ===, callback's first arg is an error object, ...
  • Comments and documentation
  • Accompanying tools, like lint checker, unit test framework, ...

This topic obviously is highly subjective, but I think it's an important step of a community to establish a common and widely accepted coding style in the process of getting mature. Also, it's not all just about taste. In particular, rules like "use === instead of ==" have a direct influence on code quality.

like image 847
alienhard Avatar asked Mar 31 '11 06:03

alienhard


People also ask

For which application NodeJS is not suitable?

Not Suitable for Heavy-Computing Apps Node. js doesn't support multi-threaded programming yet. It is able to serve way more complicated applications than Ruby, but it's not suitable for performing long-running calculations. Heavy computations block the incoming requests, which can lead to decrease of performance .

What is the best JavaScript style guide?

Airbnb JavaScript Style Guide Airbnb has one of the most popular JavaScript style guides on the internet. It covers nearly every aspect of JavaScript as well.

What is ES6 style?

JavaScript ES6 brings new syntax and new awesome features to make your code more modern and more readable. It allows you to write less code and do more. ES6 introduces us to many great features like arrow functions, template strings, class destruction, Modules… and more.


7 Answers

I'd review the coding standards checked by JSLint or look at the author of NPM (Isaac Shlueter's) coding standards.

You could also look at the style used by notable Node.JS coders:

  • TJ Holowaychuk
  • Isaac Shlueter
  • Tim Caswell
  • Jeremy Ashkenas
  • Felix Geisendörfer
  • Charlie Robbins
  • Marak Squires
  • Aaron Heckmann
  • Guillermo Rauch
  • Mikeal Rogers
  • Ryan Dahl + you could look at the actual Node.JS codebase

I'll throw mine in there for good measure ;)

Edit: Suggestions from @alienhard

  • Google JavaScript style Guide
  • Felix's Node.js Style Guide

IMO there's a few golden rules you should follow:

  • Never use with or eval
  • Use === over ==
  • Always declare your variables with var in the appropriate scope - don't fallback to the global scope
  • Wrap your app in a closure (function(){})() if you plan on releasing code that runs server-side as well as in the browser
  • Callbacks should take err as the first argument and if they themselves take a callback as an argument, it should be last, e.g. callback(err, param1, param2, callback)

Indentation, spacing between braces and keywords and semicolon placement are all a matter of preference.

like image 148
chriso Avatar answered Oct 24 '22 21:10

chriso


There's a new standard in town.

Use Standard Style.

js-standard-style

like image 20
mightyiam Avatar answered Oct 24 '22 20:10

mightyiam


You can learn a lot of good coding style practices from client side oriented JavaScript guides (most of them apply also to node.js in general since the difference between client and server side is mostly in libraries and not in language itself). For example JavaScript Patterns book dedicates to this topic some parts of the Chapter 2. Also Douglas Crockford's website, book and videos are a must see materials in order to adopt JavaScript specific coding styles and best practices I would say.

like image 35
yojimbo87 Avatar answered Oct 24 '22 19:10

yojimbo87


When using node from the terminal, it's useful for your source code to use spaces for indentation. Otherwise, the "error here" caret won't line up.

With tabs:

        var preps = files.map(function(f) { 
            ^
TypeError: Cannot call method 'map' of null

With spaces:

        var preps = files.map(function(f) { 
                          ^
TypeError: Cannot call method 'map' of null

This might be a Mac only issue, but I would suspect not.

like image 35
Daniel Yankowsky Avatar answered Oct 24 '22 20:10

Daniel Yankowsky


It has been a while since I asked this question... and in the meantime I've found this excellent JavaScript guide:

Principles of Writing Consistent, Idiomatic JavaScript

https://github.com/rwldrn/idiomatic.js/

like image 42
alienhard Avatar answered Oct 24 '22 20:10

alienhard


Airbnb has a quite good Javascript style guide https://github.com/airbnb/javascript

like image 31
Dror Avatar answered Oct 24 '22 19:10

Dror


For Coffee-Script, where bad indents means compilation errors

use

:set tabstop=2
:set shiftwidth=2
:set expandtab

popular coffee projects, zombie, brunch uses this setup for indentations.

Edit:

Actually, just use this! https://github.com/paulmillr/code-style-guides (one of the main contributors to brunch)

like image 44
Quang Van Avatar answered Oct 24 '22 20:10

Quang Van