3 Coding Standards: 3.1 Javascript
3 Coding Standards: 3.1 Javascript
3 Coding Standards: 3.1 Javascript
3.1 JavaScript
3.1.1 Namespaces
All functions and objects will belong to namespaces. The namespace should be:
qat.widget.datatable
sensus.util.actions
JavaScript code should not be embedded in HTML files unless the code is
specific to a single session.
Code in HTML adds significantly to pageweight with no opportunity for
mitigation by caching and compression.
<script src=filename.js> tags should be placed as late in the body as possible.
This reduces the effects of delays imposed by script loading on other page
components. There is no need to use the language or type attributes. It is the
server, not the script tag that determines the MIME type.
3.1.3 Indentation
Use a tab instead of spaces:
The width of a tab character can be adjusted per editor and the other
coders can view your code in the way they feel comfortable with, not in
the way you prefer.
Tabs are easy to select, because thats their sole meaning, spaces on the
other hand, have many meanings, so you cant just find & replace space
characters.
Both can produce a larger filesize, but the size is not significant over local
networks, and the difference is eliminated by minification.
Generally use line comments. Save block comments for formal documentation
and for commenting out.
//currentlyselectedtableentry
//indentationlevel
//sizeoftable
functioninner(a,b)
{
return(e*a)+b;
}
returninner(0,1);
}
This convention works well with JavaScript because in JavaScript, functions and
object literals can be placed anywhere that an expression is allowed. It
provides the best readability with inline functions and complex structures.
functiongetElementsByClassName(className)
{
varresults=[];
walkTheDOM(document.body,function(node)
{
vara;//arrayofclassnames
vari;//loopcounter
varc=node.className;//thenode'sclassname
//Ifthenodehasaclassname,thensplititintoa
listofsimplenames.
//Ifanyofthemmatchtherequestedname,thenappend
thenodetotheset
//ofresults.
if(c)
{
a=c.split('');
for(i=0;i<a.length;i+=1)
{
if(a[i]===className)
{
results.push(node);
break;
}
}
}
});
returnresults;
}
varcollection=(function()
{
varkeys=[],values=[];
return{
get:function(key)
{
varat=keys.indexOf(key);
if(at>=0)
{
returnvalues[at];
}
},
set:function(key,value)
{
varat=keys.indexOf(key);
if(at<0)
{
at=keys.length;
}
keys[at]=key;
values[at]=value;
},
remove:function(key)
{
varat=keys.indexOf(key);
if(at>=0)
{
keys.splice(at,1);
values.splice(at,1);
}
}
};
}());
3.1.8 Names
Names should be formed from the 26 upper and lower case letters (A.. Z, a..
z), the 10 digits (0 .. 9), and _ (underbar). Avoid use of international
characters because they may not read well or be understood everywhere. Do
not use $(dollar sign) or \(backslash) in names.
Do not use _(underbar) as the first character of a name. It is sometimes used
to indicate privacy, but it does not actually provide privacy. If privacy is
important, use the forms that provide private members. Avoid conventions that
3.1.9 Statements
3.1.10 Simple Statements
Each line should contain at most one statement. Put a ;(semicolon) at the end
of every simple statement. Note that an assignment statement which is
assigning a function literal or object literal is still an assignment statement and
must end with a semicolon.
JavaScript allows any expression to be used as a statement. This can mask
some errors, particularly in the presence of semicolon insertion. The only
expressions that should be used as statements are assignments and
invocations.
3.1.12 Labels
Statement labels are optional. Only these statements should be labeled: while,
do, for, switch.
3.1.14 if Statement
The ifclass of statements should have the following form:
if(condition)
{
statements;
}
if(condition())
{
statements;
}
if(condition())
{
statements;
}
else
{
statements;
}
if(condition)
{
statements;
}
elseif(condition())
{
statements;
}
else
{
statements;
}
if((condition)&&(condition)
&&(condition)&&(condition))
{
statements;
}
if((condition()&&condition())
||(condition()&&condition()))
{
statements;
The first form should be used with arrays and with loops of a predeterminable
number of iterations.
The second form should be used with objects. Be aware that members that are
added to the prototype of the object will be included in the enumeration. It is
wise to program defensively by using the hasOwnProperty method to
distinguish the true members of the object:
for(variableinobject)
{
if(object.hasOwnProperty(variable))
{
statements
}
}
3.1.17 do Statement
Unlike the other compound statements, the dostatement always ends with a ;
(semicolon).
}
finally
{
statements
}
3.1.22 Whitespace
Blank lines improve readability by setting off sections of code that are logically
related.
Blank spaces should be used in the following circumstances:
A blank space should not be used between a function value and its ((left
parenthesis). This helps to distinguish between keywords and function
invocations.
All binary operators except .(period) and ((left parenthesis) and [(left
bracket) should be separated from their operands by a space.
No space should separate a unary operator and its operand except when
the operator is a word such as typeof.
3.1.23 {} and []
Use {}instead of newObject(). Use []instead of newArray().
Use arrays when the member names would be sequential integers. Use objects
when the member names are arbitrary strings or names.
oObject1={
parameter1
param2
parameter3
};
:value,
:value,
:value
aArray:[
"VALUE1",
"VALUE2",
"VALUE3"
];
oObject2={
aArray1:aArray,
aArray2:[
"VALUE1",
"VALUE2",
"VALUE3"
]
}
is better written as
total=subtotal+(+myInput.value);
3.2 CSS
3.2.1 CSS Version
Since we are targeting IE9+, FF4+, Chrome 10+ CSS 3.0 can be used.
Add opening curly bracket on the same line as the selector identifier.
Common designs for inputs, buttons, form and other html elements for
whole page or application should be placed at the top of the stylesheet
Group the rules for same type of elements and its pseudo-elements
together.
Use spacing while describing attribute of a class to improve readability.
Use empty line between different CSS class to improve readability.
For visual emphasis use space to separate property and value.
Properties should be in alphabetical order.
Try to use shorthand property instead of long property because it will
increase readability. Use background-color, background-position,
background-image, and background-repeat etc. instead of background.
Use em instead of px for sizing if possible, line heights etc to allow
visitors to use the built-in browser text sizing.
Use semicolon at the end of each statement even if it is not required.
Try to avoid presentation specific words as identifier, like black, green
etc.