Best Practice Codeguidelines for Node.JS – Server-side Javascript

By Mai 12, 2015 Aktuelles No Comments

Spoiler: Dieser Artikel ist für ein internationales Publikum verfasst und daher auf Englisch. Wünschen Sie sich eine Deutsche Version, schreiben Sie gern ein Kommentar.

After almost two years node.js with mongodb and mysql in production I want to share some learning for better code structure and maintainability. To give a short wrap up, please read the following:

  1. The Design Guides of Node.JS
  2. Structuring Code with Promises
  3. Extended Control Flow With Promises

Design Guidelines (basics)

After writing the guidelines from my own experience I came over the one of Felix Geisendörfer, which I support and were quite similar to mine. Nevertheless here is a summary of this guidelines:

2 Spaces for indention  Self explaining
Newlines  are always in unix style (\n instead of \n\r)
No trailing whitespace  Self explaining
Use Semicolons   Self explaining
80 characters per line   Self explaining
Use single quotes  Self explaining
Opening braces go on the same line   Self explaining
Method chaining

The right way:

Declare one variable per var statement  Self explaining
Use lowerCamelCase for variables, properties and function names  Self explaining
Use UpperCamelCase for class names  Self explaining
Use UPPERCASE for Constants  Self explaining
Object / Array creation [see here] Use the === operator
Use multi-line ternary operator  (I do not support this point)
Use slashes for comments (Don’t use /* */)
Avoid: Object.freeze, Object.preventExtensions, Object.seal, with, eval
Getters and setters   Use getters, ignore setters.

 

Please find the current Version on GitHub

Leave a Reply