node.js应用程序的编码风格指南? [英] Coding Style Guide for node.js apps?

查看:118
本文介绍了node.js应用程序的编码风格指南?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js有一个(或几个)编码风格指南吗?如果没有,顶级开源节点项目使用的新兴样式是什么?

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?

我在寻找一个指南指南),它们是 PEP 8 的行,这是Python的规范编码风格指南。我看过各种JavaScript指南不值得链接这里(大多是旧的和针对客户端JavaScript)。我发现了一个有趣的node.js 样式指南

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):


  • 代码布局:缩进(2个空格,4个空格,制表符,...),换行符,换行符等。

  • 空格,例如function(arg)vs.function(arg)

  • 分号或无分号,var声明...

  • 命名,例如do_this()vs doThis(),var_name vs. varName,...

  • node.js和JavaScript成语,例如== vs. ===,callback的第一个arg是一个错误对象,...

  • / li>
  • 附带的工具,如lint checker,单元测试框架,...

  • 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, ...

是非常主观的,但我认为这是一个社区的重要一步,建立一个共同的和广泛接受的编码风格在成熟的过程。此外,这不是所有的味道。特别是,像use ===而不是==这样的规则对代码质量有直接的影响。

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.

推荐答案

d查看 JSLint 检查的代码标准,或查看 NPM (Isaac Shlueter's)编码标准

I'd review the coding standards checked by JSLint or look at the author of NPM (Isaac Shlueter's) coding standards.

您还可以查看Node.JS编码器使用的样式:

You could also look at the style used by notable Node.JS coders:

  • TJ Holowaychuk
  • Isaac Shlueter
  • Tim Caswell
  • Jeremy Ashkenas
  • Felix Geisendörfer
  • Charlie Robbins
  • Marak Squires
  • Aaron Heckmann
  • Guillermo Rauch
  • Mikeal Rogers
  • Ryan Dahl + you could look at the actual Node.JS codebase

我会抛弃我的在那里很好的措施;)

I'll throw mine in there for good measure ;)

编辑:来自 @alienhard 的建议

  • Google JavaScript style Guide
  • Felix's Node.js Style Guide


  • 不要使用 eval

  • 使用 === ==

  • 始终在适当的范围中使用 var 声明变量 - 不要全局范围

  • 如果您计划发布运行服务器的代码,则将应用程序包装在封闭(function(){})()以及在浏览器中

  • 回调应该以 err 作为第一个参数,如果他们自己接受回调作为参数,它应该是最后,例如 callback(err,param1,param2,callback)

  • Never use with or eval
  • Use === over ==
  • Always declare your variables with var in the appropriate scope - don't fallback to the global scope
  • Wrap your app in a closure (function(){})() if you plan on releasing code that runs server-side as well as in the browser
  • Callbacks should take err as the first argument and if they themselves take a callback as an argument, it should be last, e.g. callback(err, param1, param2, callback)

大括号和关键字以及分号放置都是优先选择。

Indentation, spacing between braces and keywords and semicolon placement are all a matter of preference.

这篇关于node.js应用程序的编码风格指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆