dojo 参照Java 编码规范,发布了一份 Javascript Programming Conventions Guide。
规范内容非常详细,从各种命名规范到空白的设置。对很多正尝试Javascript编程的Web开发者来说,这是一份好的参考。
顺便提一提的就是, Chryler 在他的Blog: Top Ten of Programming Advice to NOT follow 里就有一条是关于编码规范 —— Make sure your team shares a common coding standard(确定开发团队共同遵守一份编码标准) —— 注意这里是反义,他的解释是说编码规范并非人们所强调的那么重要,苛刻地要求遵守规范或许只是你的审美观的怪癖。
就我的经验,最糟糕的源码通常都是缩进的问题(所以在Eclipse里我最常用Ctrl+Shift+F),至于细节的比如空白或者临时变量定义等等坦白说无伤大雅。
This document follows the basic outline of the Java Programming Conventions Guide, a copy of which may be found at http://geosoft.no/javastyle.html.
Any violation to this guide is allowed if it enhances readability.
Guidelines in this document are informed by discussions carried out among the Dojo core developers. The most weight has been given to considerations that impact external developer interaction with Dojo code and APIs. Rules such as whitespace placement are of a much lower order importance for Dojo developers, but should be followed in the main in order to improve developer coordination.
Table of core API naming constructs:
Construct Convention Comment package lower never multiple words class UpperLower public method lowerUpper whether class or instance method. lower_case() is acceptable only if the particular function is mimicing another API. public var lowerUpper constant UpperLower or UPPER_LOWER
Table of constructs that are not visible in the API, and therefore are optional and carry less weight of enforcement.
Construct Convention Comment private method _lowerUpper private var _lowerUpper method args _lowerUpper, lowerUpper local vars _lowerUpper, lowerUpper
Names representing packages SHOULD be in all lower case.
Names representing types (classes) MUST be nouns and written in UpperLower case:
Account, EventHandler
Constants SHOULD be placed within a single object created as a holder for constants, emulating an Enum; the enum SHOULD be named appropriately, and members SHOULD be named using either UpperLower or UPPER_LOWER case:
var NodeTypes = { Element : 1, DOCUMENT: 2 }
Abbreviations and acronyms SHOULD NOT be uppercase when used as a name:
getInnerHtml(), getXml(), XmlDocument
Names representing methods SHOULD be verbs or verb phrases:
obj.getSomeValue()
Public class variables MUST be written using upperLower case.
Private class variables MAY be written using _upperLower (with preceding underscore):
var MyClass = function(){ var _buffer; this.doSomething = function(){ }; }
Variables that are intended to be private, but cannot be based on the semantics of Javascript, SHOULD prepended with a "_" (underscore) char:
this._somePrivateVariable = statement ;
NB Note that the above variable also follows the convention for a private variable.
Generic variables SHOULD have the same name as their type:
setTopic(topic) // where topic isTypeOf Topic
All names SHOULD be written in English.
Variables with a large scope SHOULD have globally unambiguious names, ambiguity MAY be distinguished by package memebership. Variables with small or private scope MAY be more terse still.
The name of the return object is implicit, and SHOULD be avoided in a method name:
getHandler(); // NOT getEventHandler()
Public names SHOULD be as clear as necessary and SHOULD avoid unclear shortenings and contractions:
MouseEventHandler, NOT MseEvtHdlr.
Note that, again, any context that can be determined by package membership SHOULD be used when determining if a variable name is clear. For example, a class that represents a mouse event handler:
dojo.events.mouse.Handler, NOT dojo.events.mouse.MouseEventHandler
Classes/constructors MAY be named based on their inheritance pattern, with the base class to the right of the name:
EventHandler UIEventHandler MouseEventHandler
NB The base class CAN be dropped from a name if it is obviously implicit in the name:
MouseEventHandler as opposed to MouseUIEventHandler.
The terms get/set SHOULD NOT used where a field is accessed, unless the variable being accessed is lexically private.
"is" prefix SHOULD be used for boolean variables and methods NB. Alternatives include "has", "can" and "should"
The term "compute" CAN be used in methods where something is computed.
The term "find" CAN be used in methods where something is looked up.
The terms "initialize" or "init" CAN be used where an object or a concept is established.
UI Control variables SHOULD be suffixed by the control type. ex. leftComboBox, topScrollPane
Plural form MUST be used to name collections.
"num" prefix or "count" postfix SHOULD be used for variables representing a number of objects.
Iterator variables SHOULD be called "i", "j", "k", etc.
Compliment names MUST be used for compliment entities. ex. get/set, add/remove, create/destroy, start/stop, insert/delete, begin/end, etc.
Abbreviations in names SHOULD be avoided.
Negated boolean variable names MUST be avoided:
isNotError, isNotFound are unacceptable.
Exception classes SHOULD be suffixed with "Exception" or "Error" .. FIXME (trt) not sure about this?
Methods returning an object MAY be named after what they return, and methods returning void after what they do.
Class or object-per-file guidelines are not yet determined.
Tabs (set to 4 spaces) SHOULD be used for indentation.
If your editor supports "file tags", please append the appropriate tag at the end of the file enable others to effortlessly obey the correct indentation guidelines for that file:
// vim:ts=4:noet:tw=0:
The incompleteness of split line MUST be made obvious:
var someExpression = Expression1 + Expression2 + Expression3 ; var o = someObject.get( Expression1, Expression2, Expression3 );
Note the indentation for expression continuation is indented relative to the variable name, while indentation for parameters is relative to the method being called.
Note also the position of the parenthesis in the method call; positioning SHOULD be similar to the use of block notation.
Block statements. 1. Block layout SHOULD BE as illustrated below:
while(!isDone){ doSomething(); isDone = moreToDo(); }
If statements SHOULD have the following form:
if(someCondition){ statements; }else if(someOtherCondition){ statements; }else{ statements; }
for statements SHOULD have the following form:
for(initialization; condition; update){ statements; }
while statement SHOULD follow the form in example VI.A.1.
a do...while statement SHOULD have the following form:
do{ statements; }while (condition);
a switch statement SHOULD have the following form:
switch (condition){ case ABC: statements; // fallthrough case DEF: statements; break; default : statements; break; }
A try...catch...finally statement SHOULD have the following form:
try{ statements; }catch (ex){ statements; }finally{ statements; }
Single statement if-else, while or for MUST NOT be written without brackets, but CAN be written on the same line:
if(condition){ statement; } while(condition){ statement; } for(intialization; condition; update){ statement; }
Whitespace
Comments