Professional Documents
Culture Documents
CLR SourceCodeConventions 111215 1327 164
CLR SourceCodeConventions 111215 1327 164
Code Structure
Integrated Development Environment (IDE) options and settings
Statements
Block Indentation
Operators
Conditional operator (if-else)
Multi-case Conditional operator (switch)
Loop operator (while, do-while)
Loop statements (for)
Boolean Statements in operators
Clauses and declarations
Constants
Variables
Braces
Method Declaration
Method definition
Return Statements
Exception Handling
Method calling
Comments
Documentation Comments
Class description block
Method description block
Property description block
Demandware script description block
Tags description
Pipeline description block
Implementation Comments
Comments around large blocks of code
Commented code
Useless comments
This guideline includes some rules for code structure display, operators’ syntax and blocks ordering as well as syntax and code style for source
code clauses and declarations.
Code Structure
Integrated Development Environment (IDE) options and settings
It is strongly recommended for the developers to use common rules of IDE settings and code style. This is very useful for people, who work in
team and often exchange some experience and consult each other on their workstations.
The developers in one team should use common IDE settings (including coloring and short-keys) and each developer should adopt his/her habits
to the common working environment defined. Team members should feel comfortable on their team mates IDE. For this reasons, the following
rules below should be applied on each developer’s IDE:
Source code should always be viewed in fixed font style (Courier like font)
Max line size should be defined where applicable. Most of the IDEs support a markup vertical line, which can be calibrated. Source code
line should not exceed 120 characters (with the block indentation, counted as 4 characters).
Tab size must be set to 4 spaces. This setting is available as an IDE option.
Statements
Statements are building blocks of the program source code. They should be as simple as possible, easy readable, clear and understood between
all members of the team. By following these guidelines, current standard introduces list of rules for statement creation and usage, presented
below:
Block Indentation
Block statement is represented by opening '{' and closing '}' brackets in the C-style languages.
For function declarations indentation style, known as “Align brackets”, is used. It uses following rules for block indentation:
Opening and closing brackets are indented always on the same vertical line
Lines with brackets do not contain any statements
Statements within the block are indented with one tab
For all other block declarations opening bracket is at the end of the previous line.
1234567890123456789012345678901234567890123456789
01 function functionName(aNumber : Number, aStartDate : Date) : Boolean
02 {
02 if (condition) {
03 statements;
04 } else {
05 statements;
06 }
07 }
Operators
Conditional operator (if-else)
Current standard introduces following rules:
There is a space between the “if” and the opening brace “(“
There are no spaces between the braces and the condition itself
The else statement appears on the same line as the closing bracket.
When the condition is complex and does not fit in the line, we can break it leaving the Boolean operator on the new line, indented once
(just after the bracket on the previous line)
When there is a single statement after the “if”, the statement should be placed indented on a new line inside brackets.
Avoid complex boolean statements. For more information see “Boolean Statements” section
case 2:
statements_2;
break;
case 3:
case 4:
statements_3;
break;
...
default:
statements_4;
}
...
There is a space between the while statement and the Boolean condition in brackets
The while operator always has a block, even the loop body is a single statement. The one-line shorthand in the form while (condition)
loop_statement; is forbidden.
In case of do-while statement, the while statement lies on the line with the closing block, separated with one space.
...
while (boolean_condition) {
loop_statements_1;
}
...
do {
loop_statements_2;
} while (boolean_condition)
...
...
for (init_stm; cond_stm; loop_stm) {
loop_statements;
}
...
Variables
Variables are data identifiers, used to refer to specific values that are generated in a program. The term “variable” also denotes class members
(static or not static) in Object Oriented Programming. Current section includes some rules for variable definition. These rules are listed below:
Each variable should be defined on a single line. The shorthand var a1, a2; is allowed only when there's no initialization of variables on
the line. The shorthand var a1 = 0, a2; is forbidden.
Local functional variables should always be initialized upon their declaration.
Class member variables (static and non-static) can be initialized either in the declaration statement or in a constructor method
When declaring a variable always give its type (String, Number, Object, etc.)
When assigning a value to a variable there must be a space before the "=" and a space after it.
...
}
Braces
After opening and before closing braces there should not be any spaces.
Example:
There are exceptions: when imprting packages and scripts, the declaration of the function execute for pipelet scripts.
importPackage( dw.customer );
importScript( 'ecm_utils:library/common/libContext.ds' );
Method Declaration
Method represents a source code structure, which defines the behavior of an object in Object Oriented Programming. The term method applies
also to functions and procedures, without any matter of the scope (class method, inner class method, global or local function and procedures).
Method’s declaration consists of defining the method signature (interface part) – return type, name, parameters. The method definition addresses
the method body (the implementation).
There should be 2 empty lines before a method with declaration and definition.
The method declaration should be indented according to the indentation of the scope/block.
Name and the first parameter are placed on the same line.
Return type is placed on the same line with the closing parenthesis of the parameter list.
If the parameter list is short, all the other parameters can be enlisted on the same line. The line should not exceed the Max Line Length
(120 chars). There should be a space after each comma in the parameter list.
If the parameter list is long or the parameters count exceeds 5, the parameters should be placed vertically, each on separate line. The
horizontal alignment of the following lines is according to the first parameter or 8 spaces after method declaration in case when method
name is very long. All parameters should be in one vertical column.
Method definition
The method definition is the implementation part of the method or so called “method body”. It can contain zero, one or more statements and
should conform to the following rules:
Opening block is below the method declaration and has the same indentation
Statements are indented with one tab (as normal block indentation)
Return Statements
The methods, considered as functions, have a return value, defined by the return operator. Current standard applies following rules for return
statements:
The return operator should always return a value, stored in a method local variable.
The count of return statements in a method should be as minimal as possible.
Comments: Many return statements make the method harder for visual verification. Complex return statements make tracing and debugging a
difficult process
Exception Handling
Example: Exceptions handling
...
try {
statements;
} catch(err) {
handling_statements;
} finally {
final_statements;
}
...
Method calling
Current standard requires following rules for method calling:
Rules exceptions: The second rule can be omitted, in case the parameter list looks short and clear.
Comments
Current standard defines some rules for several types of comments, listed in the current section.
Documentation Comments
The documentation comments are describing the purpose of a module / class / method / variable / constant definition or declaration. They
should act as a short usage manual of the item.
This type of comments should be developed according to each item importance.
/**
* This is the description for my class.
*
* @class MyClass
* @constructor
*/
/**
* My method description. Like other pieces of your comment blocks,
* this can span multiple lines.
*
* @method methodName
* @param {String} foo Argument 1
* @param {Object} config A config object
* @param {String} config.name The name on the config object
* @param {Function} config.callback A callback function on the config object
* @param {Boolean} [extra=false] Do extra, optional work
* @return {Boolean} Returns true on success
*/
/**
* My property description. Like other pieces of your comment blocks,
* this can span multiple lines.
*
* @property propertyName
* @type {Object}
* @default "foo"
*/
Tags description
Primary Tags
@class IO
@constructor
**/
function IO (config)
{
Secondary tags
@property SRC_REPLACE
@type String
@static
@final
**/
@attribute extras
@type {Object} extra
data
@optional
**/
@attribute url
@type {String} url to
fetch remote data
from
@required
**/
param Defines a parameter for an ordinary
/** @method, a parameter for a @constructor
@param {String} name (generally defined inside a @class block), or
An a property that resides
Attribute name or on an @event object. Can take either of the
object property forms:
path. @param {type} name description
**/ @param name {type} description
The {type} is optional, but if you include it,
/** you must surround
@param {Object} it in curly braces so that it is distinguished
[options] Data from the name.
to be mixed into The name also has optional syntax:
the event [name] — optional parameter
facade of the [name=foo] — default value is foo
`change` name* — placeholder for 1..n args
event(s) for these [name]* — placeholder for 0..n args
attributes. As shown in the example, you can also nest
@param {Boolean} @param tags.
[options.silent] This enables you to document object
If `true`, no parameters that have their own particular
nested structure.
`change` event
will be fired.
**/
@private
@property _jsonp
**/
Above each pipeline start node there must be a comment block describing the purpose of the pipeline. The comment should include INPUT and
OUTPUT information of the pipeline.
It is recommended to fill the "Custom Label" and "Description" fields of the pipeline nodes.
Implementation Comments
The implementation comments are the comments, found in method body implementation.
In general, the source code implementation should be self-documented. But there are always cases, where comments should be explicitly
applied:
In case of reasonable irregular operator usage (complex switch operators, complex statements). The reason should be specified.
In case of strange workaround/fix or irregular action – The reason should be specified!
In case of reasonable complex statements – the reason and the statement description should be specified.
In case there is an operator with a block of code that does not fit in a normal screen, it should end with a comment after the closing bracket "}".
switch (bar)
{
...
// very long block of code
...
} // END switch (bar)
Long sections of code for a single functionality that do not fit in a normal screen should be surraounded with comments indicating the start and the
end of the section.
...
...
Commented code
Piece of code can be temporary commented during testing or debugging. In general, no commented code should be left when claiming the source
file is ready (during check-in).
In case of any reasons to leave a piece of code commented, specify and describe in another comment why the code is left commented.
Commented code without such “reasonable comments” should be discarded by every developer upon editing the source file. Commented code
with “reasonable comments” should not be discarded by everyone.
Useless comments
There are comments, which are not describing more than the code itself. Such comments are useless and should be discarded.