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

2019-01-20 21:27发布

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.

7条回答
Animai°情兽
2楼-- · 2019-01-20 21:56

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.

查看更多
登录 后发表回答