Write in a conversational style.
- Use pronouns: I, we, you, they.
- Use colloquial expressions: a sure thing, turn-on, rip-off, OK.
- Use contractions: they're, you're, it's, here's, we've, I'm.
- Use simple words.
- If you must choose between writing naturally and being grammatically correct, write naturally.
// yay I'm going home. It's simple. // nay I am going home. It is simple.
Define
varone at a time (don't use leading/trailing commas):// yayvarevent=require('event');vardom=require('dom');varassert=require('assert');// nayvarevent=require('event'),dom=require('dom'),assert=require('assert');// nayvarevent=require('event'),dom=require('dom'),assert=require('assert');
While the later two are minimally more optimized, minifiers like uglifyjs will make these optimizations for you. So write what is most human readable.
Use trailing commas for multi-line arrays.
// yayvarevents=['click','keypress','focusin','focusout'];// nayvarevents=['click','keypress','focusin','focusout'];
Use trailing commas for multi-line objects.
// yayvarstyles={color: '#000',background: '#fff',width: 100,height: 200};// nayvarstyles={color: '#000',background: '#fff',width: 100,height: 200};
Always use
"double quotes when declaring JSON keys and values.// yay{"myKey": "string","myArray": ["some stuff","more stuff"]}// nay{myKey: 'string','myArray': ['some stuff','more stuff']}
For strings that are not defined in JSON, default to declaring strings with
'single quotes. In the case where your string contains special format characters or'single quotes, use"double quotes.// yayvarstr='"Marty" is at the party!';varspecialStr="im'oto";// nayvarstr="\"Marty\" is at the party!";varspecialStr='im\'oto';
For assigned functions, have no whitespace between parentheses and brackets:
){vs.) {.// yayexports.init=function(){this.x;};// nayexports.init=function(){this.x;};
For callback functions, do the same thing.
// yayroute('/users').on('exec',function(){});// nayroute('/users').on('exec',function(){});
For non-assigned functions, do it the other way around.
// yayfunctioninit(){this.x;};// nayfunctioninit(){this.x;};
This distinguishes the function declarations and function expressions.
Indent DSL methods if they are in a new object scope.
// yayadapter('facebook').model('user').attr('username').action('create').action('find').param('page','integer').model('like');// nayadapter('facebook').model('user').attr('username').action('create').action('find').param('page','integer').model('like');
Keep 1 space between native methods and parentheses (
if,while,for, etc.).// yayif(x)y();// nayif(x)y();
Emit event names in the present tense as
verb [object|property].// yaythis.emit('define',user);this.emit('define user',user);// naythis.emit('defined user',user);this.emit('user defined',user);this.emit('user define',user);
Emit namespaced events from most generic to most specific. This way, you can mixin more generic stuff first, and use those generic methods on specific objects.
// yaythis.emit('define',user);this.emit('define facebook',user);this.emit('define facebook.user',user);// naythis.emit('define facebook.user',user);this.emit('define facebook',user);this.emit('define',user);
Pass the
thisascontextfor the first parameter in DSL methods, instead of usingthisas a reference. The context is stated more clearly.// yayroute('/users').on('request',function(context,next){context.render();});// nayroute('/users').on('request',function(next){this.render();});
Place the compared value on the left instead of the right. This is unnatural at first, but it makes the code much easier to read for a newcomer.
// yayif('string'===typeof(x))exec();// nayif(typeof(x)==='string')exec();
The reason for this is, the compared-to value (e.g.
'string') is first, so it readsif ('string') exec().Leave 1 empty line at the *top of each file.
\n /** * Blank line above. */
/** * No blank line above */
Leave 0 empty lines at the bottom of each file.
Leave 1 blank line between comments and code.
/** * GOOD */exports.init=function(){};
/** * BAD */exports.init=function(){};
For loops should be as efficient and clean as possible.
// yayfor(vari=0,n=arr.length;i<n;i++){}// nayfor(vari=0;i<arr.length;i++){}
Single letter variables should only be used within loops (basically for loops).
// yayfor(vari=0,n=arr.length;i<n;i++){}// nayfor(varindex=0,size=arr.length;index<size;index++){}
For each loops (for loops but with objects) should have safe guards, which is a general good JavaScript practice.
// yayfor(varkeyinobj){if(obj.hasOwnProperty(key)){deleteobj[key];}}// nay for(varkeyinobj){deleteobj[key];}
Use
obj.init()instead ofnew Obj, whereobj.create()should do some db/service call.// yayvarnewObj=obj.init();// nayvarnewObj=newObj();
For single-line arrays and objects, put one space before/after containing brackets.
// yay['i386','x86_64']{status: 'active'}// nay['i386','x86_64']{status: 'active'}
Avoid aligning signs such as
=,:, etc. The reason is, while it does create some symmetry, it makes the code a little harder to read.// yayvarx=1;varfoo='bar';varhello='world';// nayvarx=1;varfoo='bar';varhello='world';
If a ternary statement is too long, put it on multiple lines with
?and:at the start of the line.// yayexports.query=function(name){returnnull==name ? query().start(this.className) : query(name);}// nayexports.query=function(name){returnnull==name ? query().start(this.className) : query(name);}
If you are building a multiline string, use
+at the beginning of the line.// yayvarstring='some '+'long '+'string ';// nayvarstring='some '+'long '+'string ';// nayvarstring='some \ long \ string ';
In EcmaScript 6, you'll be able to use backticks for multi-line strings.
- Name your projects using lowercase with hyphens.
// yayvarmyProject=require('my-project');// nayvarmyProject=require('myProject');varmyProject=require('MyProject');
These are all of the events used in Tower. When creating custom APIs, see if these events can be used before defining another. Maybe we can even cut it down.
emit('define');emit('init');emit('exec');emit('open');emit('close');emit('connect');emit('disconnect');emit('render');emit('request');emit('error');emit('data');Maybe route.on('request') becomes route.on('exec'). And route.on('connect') becomes route.on('open').
- Use easy to scan/understand syntax for assertions.
// yayassert(true===val);assert(false===val);assert('foo'===val);assert(undefined===val);// nayassert.isTrue(val);assert.isFalse(val);assert.equal('foo',val);assert.isUndefined(val);// - should syntaxval.should.be(undefined);
- Here is the general structure of a good comment:
/** * Iterate each value and invoke `fn(val, i)`. * * users.each(function(val, i){ * * }); * * @param {Function} fn * @return {Object} self * @api public */proto.forEach=function(fn){// ...returnthis;};
The basic format:
- Title
- Installation
- Examples (optional)
- API
- Running Tests
- Contributing
- Notes (optional)
- License
# Title
Quick description (1-2 sentences).
## Installation
node.js:
```
npm install tower-project
```
browser:
```
component install tower/project
```
## Example
```js
var project = require('tower-project');
// ...
```
## API
Optional brief intro.
### .on(event, fn, [capture])
Short description of some method:
```js
dom('a.remove').on('click', function(e){
});
```
### .on(event, selector, fn, [capture])
Bind delegate `event` handler function for `selector`:
```js
dom('ul li').on('click', 'a.remove', function(e){
});
```
## Running Tests
Install testem:
```bash
$ npm install -g testem
```
Run tests:
```bash
$ testem
```
Then, open all the browsers you want to test by going to the outputted url defaulted to [http://localhost:7357](http://localhost:7357)
Tests will run on any open browser linked to the stated url and your current node environment.
## Contributing
Before you send a pull request, make sure your code meets the style guidelines at [https://github.com/tower/style-guide](https://github.com/tower/style-guide) and all tests pass.
## Notes
This section is optional, but where you'd write whatever, like design decisions, when to use, overview, etc. Here you could go into the theory too (distributed data, etc.).
## Licence
MIT
- https://github.com/component/struct/blob/master/index.js
- https://github.com/component/pipe/blob/master/index.js
- https://github.com/component/raf/blob/master/index.js
- https://github.com/component/to-function/blob/master/index.js
- https://github.com/component/enumerable/blob/master/index.js
- https://github.com/component/network
- https://github.com/component/trace/blob/master/index.js
- http://stackoverflow.com/questions/5495984/coding-style-guide-for-node-js-apps