]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/html/doc/misc/npm-coding-style.html
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / html / doc / misc / npm-coding-style.html
1 <!doctype html>
2 <html>
3   <title>npm-coding-style</title>
4   <meta charset="utf-8">
5   <link rel="stylesheet" type="text/css" href="../../static/style.css">
6   <link rel="canonical" href="https://www.npmjs.org/doc/misc/npm-coding-style.html">
7   <script async=true src="../../static/toc.js"></script>
8
9   <body>
10     <div id="wrapper">
11
12 <h1><a href="../misc/npm-coding-style.html">npm-coding-style</a></h1> <p>npm&#39;s &quot;funny&quot; coding style</p>
13 <h2 id="description">DESCRIPTION</h2>
14 <p>npm&#39;s coding style is a bit unconventional.  It is not different for
15 difference&#39;s sake, but rather a carefully crafted style that is
16 designed to reduce visual clutter and make bugs more apparent.</p>
17 <p>If you want to contribute to npm (which is very encouraged), you should
18 make your code conform to npm&#39;s style.</p>
19 <p>Note: this concerns npm&#39;s code not the specific packages that you can download from the npm registry.</p>
20 <h2 id="line-length">Line Length</h2>
21 <p>Keep lines shorter than 80 characters.  It&#39;s better for lines to be
22 too short than to be too long.  Break up long lists, objects, and other
23 statements onto multiple lines.</p>
24 <h2 id="indentation">Indentation</h2>
25 <p>Two-spaces.  Tabs are better, but they look like hell in web browsers
26 (and on GitHub), and node uses 2 spaces, so that&#39;s that.</p>
27 <p>Configure your editor appropriately.</p>
28 <h2 id="curly-braces">Curly braces</h2>
29 <p>Curly braces belong on the same line as the thing that necessitates them.</p>
30 <p>Bad:</p>
31 <pre><code>function ()
32 {
33 </code></pre><p>Good:</p>
34 <pre><code>function () {
35 </code></pre><p>If a block needs to wrap to the next line, use a curly brace.  Don&#39;t
36 use it if it doesn&#39;t.</p>
37 <p>Bad:</p>
38 <pre><code>if (foo) { bar() }
39 while (foo)
40   bar()
41 </code></pre><p>Good:</p>
42 <pre><code>if (foo) bar()
43 while (foo) {
44   bar()
45 }
46 </code></pre><h2 id="semicolons">Semicolons</h2>
47 <p>Don&#39;t use them except in four situations:</p>
48 <ul>
49 <li><code>for (;;)</code> loops.  They&#39;re actually required.</li>
50 <li>null loops like: <code>while (something) ;</code> (But you&#39;d better have a good
51 reason for doing that.)</li>
52 <li><code>case &quot;foo&quot;: doSomething(); break</code></li>
53 <li>In front of a leading <code>(</code> or <code>[</code> at the start of the line.
54 This prevents the expression from being interpreted
55 as a function call or property access, respectively.</li>
56 </ul>
57 <p>Some examples of good semicolon usage:</p>
58 <pre><code>;(x || y).doSomething()
59 ;[a, b, c].forEach(doSomething)
60 for (var i = 0; i &lt; 10; i ++) {
61   switch (state) {
62     case &quot;begin&quot;: start(); continue
63     case &quot;end&quot;: finish(); break
64     default: throw new Error(&quot;unknown state&quot;)
65   }
66   end()
67 }
68 </code></pre><p>Note that starting lines with <code>-</code> and <code>+</code> also should be prefixed
69 with a semicolon, but this is much less common.</p>
70 <h2 id="comma-first">Comma First</h2>
71 <p>If there is a list of things separated by commas, and it wraps
72 across multiple lines, put the comma at the start of the next
73 line, directly below the token that starts the list.  Put the
74 final token in the list on a line by itself.  For example:</p>
75 <pre><code>var magicWords = [ &quot;abracadabra&quot;
76                  , &quot;gesundheit&quot;
77                  , &quot;ventrilo&quot;
78                  ]
79   , spells = { &quot;fireball&quot; : function () { setOnFire() }
80              , &quot;water&quot; : function () { putOut() }
81              }
82   , a = 1
83   , b = &quot;abc&quot;
84   , etc
85   , somethingElse
86 </code></pre><h2 id="whitespace">Whitespace</h2>
87 <p>Put a single space in front of ( for anything other than a function call.
88 Also use a single space wherever it makes things more readable.</p>
89 <p>Don&#39;t leave trailing whitespace at the end of lines.  Don&#39;t indent empty
90 lines.  Don&#39;t use more spaces than are helpful.</p>
91 <h2 id="functions">Functions</h2>
92 <p>Use named functions.  They make stack traces a lot easier to read.</p>
93 <h2 id="callbacks-sync-async-style">Callbacks, Sync/async Style</h2>
94 <p>Use the asynchronous/non-blocking versions of things as much as possible.
95 It might make more sense for npm to use the synchronous fs APIs, but this
96 way, the fs and http and child process stuff all uses the same callback-passing
97 methodology.</p>
98 <p>The callback should always be the last argument in the list.  Its first
99 argument is the Error or null.</p>
100 <p>Be very careful never to ever ever throw anything.  It&#39;s worse than useless.
101 Just send the error message back as the first argument to the callback.</p>
102 <h2 id="errors">Errors</h2>
103 <p>Always create a new Error object with your message.  Don&#39;t just return a
104 string message to the callback.  Stack traces are handy.</p>
105 <h2 id="logging">Logging</h2>
106 <p>Logging is done using the <a href="https://github.com/npm/npmlog">npmlog</a>
107 utility.</p>
108 <p>Please clean up logs when they are no longer helpful.  In particular,
109 logging the same object over and over again is not helpful.  Logs should
110 report what&#39;s happening so that it&#39;s easier to track down where a fault
111 occurs.</p>
112 <p>Use appropriate log levels.  See <code><a href="../misc/npm-config.html">npm-config(7)</a></code> and search for
113 &quot;loglevel&quot;.</p>
114 <h2 id="case-naming-etc-">Case, naming, etc.</h2>
115 <p>Use <code>lowerCamelCase</code> for multiword identifiers when they refer to objects,
116 functions, methods, properties, or anything not specified in this section.</p>
117 <p>Use <code>UpperCamelCase</code> for class names (things that you&#39;d pass to &quot;new&quot;).</p>
118 <p>Use <code>all-lower-hyphen-css-case</code> for multiword filenames and config keys.</p>
119 <p>Use named functions.  They make stack traces easier to follow.</p>
120 <p>Use <code>CAPS_SNAKE_CASE</code> for constants, things that should never change
121 and are rarely used.</p>
122 <p>Use a single uppercase letter for function names where the function
123 would normally be anonymous, but needs to call itself recursively.  It
124 makes it clear that it&#39;s a &quot;throwaway&quot; function.</p>
125 <h2 id="null-undefined-false-0">null, undefined, false, 0</h2>
126 <p>Boolean variables and functions should always be either <code>true</code> or
127 <code>false</code>.  Don&#39;t set it to 0 unless it&#39;s supposed to be a number.</p>
128 <p>When something is intentionally missing or removed, set it to <code>null</code>.</p>
129 <p>Don&#39;t set things to <code>undefined</code>.  Reserve that value to mean &quot;not yet
130 set to anything.&quot;</p>
131 <p>Boolean objects are verboten.</p>
132 <h2 id="see-also">SEE ALSO</h2>
133 <ul>
134 <li><a href="../misc/npm-developers.html">npm-developers(7)</a></li>
135 <li><a href="../misc/npm-faq.html">npm-faq(7)</a></li>
136 <li><a href="../cli/npm.html">npm(1)</a></li>
137 </ul>
138
139 </div>
140
141 <table border=0 cellspacing=0 cellpadding=0 id=npmlogo>
142 <tr><td style="width:180px;height:10px;background:rgb(237,127,127)" colspan=18>&nbsp;</td></tr>
143 <tr><td rowspan=4 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td><td style="width:40px;height:10px;background:#fff" colspan=4>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=4>&nbsp;</td><td style="width:40px;height:10px;background:#fff" colspan=4>&nbsp;</td><td rowspan=4 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td><td colspan=6 style="width:60px;height:10px;background:#fff">&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=4>&nbsp;</td></tr>
144 <tr><td colspan=2 style="width:20px;height:30px;background:#fff" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:20px;height:10px;background:#fff" rowspan=4 colspan=2>&nbsp;</td><td style="width:10px;height:20px;background:rgb(237,127,127)" rowspan=2>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:20px;height:10px;background:#fff" rowspan=3 colspan=2>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td></tr>
145 <tr><td style="width:10px;height:10px;background:#fff" rowspan=2>&nbsp;</td></tr>
146 <tr><td style="width:10px;height:10px;background:#fff">&nbsp;</td></tr>
147 <tr><td style="width:60px;height:10px;background:rgb(237,127,127)" colspan=6>&nbsp;</td><td colspan=10 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td></tr>
148 <tr><td colspan=5 style="width:50px;height:10px;background:#fff">&nbsp;</td><td style="width:40px;height:10px;background:rgb(237,127,127)" colspan=4>&nbsp;</td><td style="width:90px;height:10px;background:#fff" colspan=9>&nbsp;</td></tr>
149 </table>
150 <p id="footer">npm-coding-style &mdash; npm@2.15.11</p>
151