



















Contents:
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.
Widget authors are expected to adhere to this style guide and also to the Dojo Accessibility Design Requirements guidelines.
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 |
|---|---|---|
| module | lowercase |
never multiple words |
| class | CamelCase |
|
| public method | mixedCase |
whether class or instance method. lower_case() is acceptable only if the particular function is mimicking another API. |
| public var | mixedCase |
|
| constant | CamelCase or UPPER_CASE |
Table of constructs that are not visible in the API, and therefore carry less weight of enforcement.
| Construct | Convention |
|---|---|
| private method | _mixedCase |
| private var | _mixedCase |
| method args | _mixedCase, mixedCase |
| local vars | _mixedCase, mixedCase |
When constructing string IDs or ID prefixes in the code, do not use "dojo", "dijit" or "dojox" in the names. Because we now allow multiple versions of dojo in a page, it is important you use _scopeName instead (dojo._scopeName, dijit._scopeName, dojox._scopeName).
Names representing modules SHOULD be in all lower case.
Names representing types (classes) MUST be nouns and written using CamelCase capitalization:
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 CamelCase or UPPER_CASE capitalization:
|
1 2 3 4 |
|
Abbreviations and acronyms SHOULD NOT be UPPERCASE when used as a name:
|
1 |
|
Names representing methods SHOULD be verbs or verb phrases:
Public class variables MUST be written using mixedCase capitalization.
CSS variable names SHOULD follow the same conventions as public class variables.
Private class variables MAY be written using _mixedCase (with preceding underscore):
|
1 2 3 4 5 |
|
Variables that are intended to be private, but are not closure bound, SHOULD be prepended with a "_" (underscore) char:
|
1 |
|
Note: the above variable also follows the convention for a private variable.
Generic variables SHOULD have the same name as their type:
|
1 |
|
All names SHOULD be written in English.
Variables with a large scope SHOULD have globally unambiguous names; ambiguity MAY be distinguished by module membership. Variables with small or private scope MAY have terse names.
The name of the return object is implicit, and SHOULD be avoided in a method name:
|
1 |
|
Public names SHOULD be as clear as necessary and SHOULD avoid unclear shortenings and contractions:
|
1 |
|
Note that, again, any context that can be determined by module membership SHOULD be used when determining if a variable name is clear. For example, a class that represents a mouse event handler:
|
1 |
|
Classes/constructors MAY be named based on their inheritance pattern, with the base class to the right of the name:
|
1 2 3 |
|
The base class CAN be dropped from a name if it is obviously implicit in the name:
|
1 |
|
The terms get/set SHOULD NOT used where a field is accessed, unless the variable being accessed is lexically private.
The "is" prefix SHOULD be used for boolean variables and methods. 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. Examples: leftComboBox, topScrollPane
Plural form MUST be used to name collections.
A "num" prefix or "count" postfix SHOULD be used for variables representing a number of objects.
Iterator variables SHOULD be called "i", "j", "k", etc.
Complement names MUST be used for complement entities. Examples: 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:
|
1 |
|
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 to enable others to effortlessly obey the correct indentation guidelines for that file:
The incompleteness of a split line MUST be made obvious :
|
1 2 3 4 5 6 7 8 |
|
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 2 3 4 |
|
if statements SHOULD have the following form:
|
1 2 3 4 5 6 7 |
|
for statements SHOULD have the following form:
|
1 2 3 |
|
while statements SHOULD have the following form:
|
1 2 3 4 |
|
do...while statements SHOULD have the following form:
|
1 2 3 |
|
switch statements SHOULD have the following form:
|
1 2 3 4 5 6 7 8 9 10 11 |
|
try...catch...finally statements SHOULD have the following form:
|
1 2 3 4 5 6 7 |
|
A single statement if-else, while or for MUST NOT be written without brackets, but CAN be written on the same line:
|
1 2 3 |
|
Whitespace
breakcatchcontinuedoelsefinallyforfunction if anonymous, ex. var foo = function(){};ifreturnswitchthistryvoidwhilewithcasedefaultdeletefunction if named, ex. function foo(){};ininstanceofnewthrowtypeofvardoSomething(someParameter); // NOT doSomething (someParameter)Comments
When parsing a comment block, we give the parser a list of "keys" to look for. These include summary, description, and returns, but many comment blocks will also have all of the variables and parameters in the object or function added to this list of keys as well.
If any of these keys occur at the beginning of a line, the parser will start reading the text following it and save it as part of that key until we find a completely blank line, or another key. This means that you should be careful about what word you use to start a line. For example, "summary" shouldn't start a line unless the content that follows is the summary.
The Markdown syntax is used in descriptions and code examples.
In Markdown, to indicate a code block, indent the code block using either four spaces, or a single tab. The parser considers the | (pipe) character to indicate the start of a line. You must use | followed by a tab or four spaces in order to indicate a code block.
In Markdown, to indicate an inline piece of code, surround the code with backticks. eg `<div>`.
These keys provide descriptions for the function or object:
Methods are assumed to be public, but are considered protected by default if they start with a _prefix. This means that the only time you'd use protected is if you don't want someone to use a function without a _prefix, and the only time you'd use private is if you don't want someone to touch your method at all.
|
1 2 3 4 5 6 |
|
|
1 2 3 4 5 6 7 8 |
|
dojo.connect) to receive notification that some event happened, such as a user clicking a button or an animation completing. For example:
|
1 2 3 4 5 6 7 |
|
postCreate) or methods where a subclass is expected to change some basic default functionality (e.g. buildRendering). A callback is just a notification that some event happened, an extension is where the widget code is expecting a method to return a value or perform some action. For example, on a calendar:
|
1 2 3 4 5 6 7 8 9 10 |
|
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Has no description of what it returns
|
1 2 3 4 5 6 7 8 9 |
|
If the declaration passes a constructor, the summary and description must be filled in there. If you do not pass a constructor, the comment block can be created in the passed mixins object.
For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Types should (but don't have to) appear in the main parameter definition block.
For example:
|
1 |
|
There are some modifiers you can add after the type:
? means optional... means the last parameter repeats indefinitely[] means an array|
1 |
|
If you want to also add a summary, you can do so in the initial comment block. If you've declared a type in the parameter definition, you do not need to redeclare it here.
The format for the general information is: *key *Descriptive sentence
The format for parameters and variables is: *key ~type~* Descriptive sentence
Where *key *and ~*type*~ can be surrounded by any non-alphanumeric characters.
|
1 2 3 4 5 6 |
|
Instance variables, prototype variables and external variables can all be defined in the same way. There are many ways that a variable might get assigned to this function, and locating them all inside of the actual function they reference is the best way to not lose track of them, or accidentally comment them multiple times.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Variables can be tagged by placing them in a whitespace-separated format before the type value between [ and ] characters. The tags available for variables are the same as outlined in the main tags, plus a few variable-specific additions:
attr method) will be a no-op.
|
1 2 3 |
|
|
1 2 3 |
|
The parser takes the comments in between object values and applies the same rules as if they were in the initial comment block:
|
1 2 3 4 5 6 7 |
|
Because a function can return multiple types, the types should be declared on the same line as the return statement, and the comment must be the last thing on the line. If all the return types are the same, the parser uses that return type. If they're different, the function is considered to return "mixed".
|
1 2 3 4 5 6 7 |
|
Note: The return type should be on the same line as the return statement. The first example is invalid, the second is valid:
|
1 2 3 4 5 6 7 8 9 10 |
|
Sometimes objects are constructed in a way that is hard to see from just looking through source. Or we might pass a generic object and want to let the user know what fields they can put in this object. In order to do this, there are two solutions:
There are some instances where you might want an object or function to appear in documentation, but not in Dojo, nor in your build. To do this, start a comment block with /*=====. The number of = can be 5 or more.
The parser simply replaces the /*===== and =====*/ with whitespace at the very start, so you must be very careful about your syntax.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Doing this allows us to see syntax highlighting in our text editor, and we can worry less about breaking the syntax of the file that's actually in the code-base during parsing. It's nothing more complicated that writing a normal JS file, with a dojo.provide call.
The trade-off is that it's harder to maintain documentation-only files. It's a good idea to only have one of these per the namespace depth you're at. eg in the same directory that the file you're documenting is. We'll see an example of its use in the next section.
A lot of Dojo uses keyword-style arguments (kwArg). It's difficult to describe how to use them sometimes. One option is to provide a pseudo-object describing its behavior. So we'll create module/_arg.js and do the following:
|
1 2 3 4 5 6 7 8 9 |
|
This describes a real object that mimics the functionality of the generic object you would normally pass, but also provides documentation of what fields it has and what they do.
To associate this object with the originating function, do this:
|
1 2 3 4 |
|
Since we didn't do a dojo.require on module._arg, it won't get included, but the documentation parser will still provide a link to it, allowing the user to see its functionality. This pseudo object may also be included in-line using the /*===== =====*/ syntax. For an example of how to do this inline, see "dojo.__FadeArgs" pseudo code in dojo/_base/fx.js, used to document dojo.fadeIn() and dojo.fadeOut()
Documenting in another file reduces the chance that your code will break code parsing. It's a good idea from this perspective to use the separate file style as much as possible.
There are many situations where you can't do this, in which case you should use the inline-comment syntax. There is also a fear that people will forget to keep documentation in sync as they add new invisible mixed in fields. If this is a serious concern, you can also use the inline comment syntax.
If you are a developer who has marked their code up using this syntax and want to test to make sure it is correct, you can run the doctool yourself locally. See INSTALL in util/jsdoc. There is also a tool to quickly view simple parsing found in util/docscripts/_browse.php
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。