Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2d30771089
Fetching contributors…

Cannot retrieve contributors at this time

227 lines (167 sloc) 7.32 kb
<!doctype html>
<meta http-equiv="content-type" value="text/html;utf-8">
<link rel="stylesheet" type="text/css" href="./style.css">
<div id="wrapper">
<h1><a href="coding-style.html">coding-style</a></h1> <p>npm's "funny" coding style</p>
<p>npm's coding style is a bit unconventional. It is not different for
difference's sake, but rather a carefully crafted style that is
designed to reduce visual clutter and make bugs more apparent.</p>
<p>If you want to contribute to npm (which is very encouraged), you should
make your code conform to npm's style.</p>
<h2 id="Line-Length">Line Length</h2>
<p>Keep lines shorter than 80 characters. It's better for lines to be
too short than to be too long. Break up long lists, objects, and other
statements onto multiple lines.</p>
<h2 id="Indentation">Indentation</h2>
<p>Two-spaces. Tabs are better, but they look like hell in web browsers
(and on github), and node uses 2 spaces, so that's that.</p>
<p>Configure your editor appropriately.</p>
<h2 id="Curly-braces">Curly braces</h2>
<p>Curly braces belong on the same line as the thing that necessitates them.</p>
<pre><code>function ()
<pre><code>function () {</code></pre>
<p>If a block needs to wrap to the next line, use a curly brace. Don't
use it if it doesn't.</p>
<pre><code>if (foo) { bar() }
while (foo)
<pre><code>if (foo) bar()
while (foo) {
<h2 id="Semicolons">Semicolons</h2>
<p>Don't use them except in four situations:</p>
<ul><li><code>for (;;)</code> loops. They're actually required.</li><li>null loops like: <code>while (something) ;</code> (But you'd better have a good
reason for doing that.)</li><li>case "foo": doSomething(); break</li><li>In front of a leading ( or [ at the start of the line.
This prevents the expression from being interpreted
as a function call or property access, respectively.</li></ul>
<p>Some examples of good semicolon usage:</p>
<pre><code>;(x || y).doSomething()
;[a, b, c].forEach(doSomething)
for (var i = 0; i &lt; 10; i ++) {
switch (state) {
case "begin": start(); continue
case "end": finish(); break
default: throw new Error("unknown state")
<p>Note that starting lines with <code>-</code> and <code>+</code> also should be prefixed
with a semicolon, but this is much less common.</p>
<h2 id="Comma-First">Comma First</h2>
<p>If there is a list of things separated by commas, and it wraps
across multiple lines, put the comma at the start of the next
line, directly below the token that starts the list. Put the
final token in the list on a line by itself. For example:</p>
<pre><code>var magicWords = [ "abracadabra"
, "gesundheit"
, "ventrilo"
, spells = { "fireball" : function () { setOnFire() }
, "water" : function () { putOut() }
, a = 1
, b = "abc"
, etc
, somethingElse</code></pre>
<h2 id="Whitespace">Whitespace</h2>
<p>Put a single space in front of ( for anything other than a function call.
Also use a single space wherever it makes things more readable.</p>
<p>Don't leave trailing whitespace at the end of lines. Don't indent empty
lines. Don't use more spaces than are helpful.</p>
<h2 id="Functions">Functions</h2>
<p>Use named functions. They make stack traces a lot easier to read.</p>
<h2 id="Callbacks-Sync-async-Style">Callbacks, Sync/async Style</h2>
<p>Use the asynchronous/non-blocking versions of things as much as possible.
It might make more sense for npm to use the synchronous fs APIs, but this
way, the fs and http and child process stuff all uses the same callback-passing
<p>The callback should always be the last argument in the list. Its first
argument is the Error or null.</p>
<p>Be very careful never to ever ever throw anything. It's worse than useless.
Just send the error message back as the first argument to the callback.</p>
<h2 id="Errors">Errors</h2>
<p>Always create a new Error object with your message. Don't just return a
string message to the callback. Stack traces are handy.</p>
<p>Use the <code>require("./utils/log").er</code> function. It takes a callback and an
error message, and returns an object that will report the message in the
event of a failure. It's quite handy.</p>
<pre><code>function myThing (args, cb) {
getData(args, function (er, data) {
if (er) return, "Couldn't get data")(er)
doSomethingElse(data, cb)
function justHasToWork (cb) {
doSomething(, "the doSomething failed."))
<h2 id="Logging">Logging</h2>
<p>Please clean up logs when they are no longer helpful. In particular,
logging the same object over and over again is not helpful. Logs should
report what's happening so that it's easier to track down where a fault
<p>Use appropriate log levels. The default log() function logs at the
"info" level. See <code><a href="config.html">config</a></code> and search for "loglevel".</p>
<h2 id="Case-naming-etc">Case, naming, etc.</h2>
<p>Use lowerCamelCase for multiword identifiers when they refer to objects,
functions, methods, members, or anything not specified in this section.</p>
<p>Use UpperCamelCase for class names (things that you'd pass to "new").</p>
<p>Use all-lower-hyphen-css-case for multiword filenames and config keys.</p>
<p>Use named functions. They make stack traces easier to follow.</p>
<p>Use CAPS<em>SNAKE</em>CASE for constants, things that should never change
and are rarely used.</p>
<p>Use a single uppercase letter for function names where the function
would normally be anonymous, but needs to call itself recursively. It
makes it clear that it's a "throwaway" function.</p>
<h2 id="null-undefined-false-0">null, undefined, false, 0</h2>
<p>Boolean variables and functions should always be either <code>true</code> or
<code>false</code>. Don't set it to 0 unless it's supposed to be a number.</p>
<p>When something is intentionally missing or removed, set it to <code>null</code>.</p>
<p>Don't set things to <code>undefined</code>. Reserve that value to mean "not yet
set to anything."</p>
<p>Boolean objects are verboten.</p>
<h2 id="SEE-ALSO">SEE ALSO</h2>
<ul><li><a href="developers.html">developers</a></li><li><a href="faq.html">faq</a></li><li><a href="npm.html">npm</a></li></ul>
<p id="footer">coding-style &mdash; npm@1.0.29</p>
;(function () {
var wrapper = document.getElementById("wrapper")
var els ="*"), 0)
.filter(function (el) {
return el.parentNode === wrapper
&& el.tagName.match(/H[1-6]/)
var l = 2
, toc = document.createElement("ul")
toc.innerHTML = (el) {
var i = el.tagName.charAt(1)
, out = ""
while (i > l) {
out += "<ul>"
l ++
while (i < l) {
out += "</ul>"
l --
out += "<li><a href='#" + + "'>" +
( el.innerText || el.text || el.innerHTML)
+ "</a>"
return out
}).join("\n") = "toc"
Jump to Line
Something went wrong with that request. Please try again.