-
Notifications
You must be signed in to change notification settings - Fork 0
Code Formatting JavaScript
Adroaldo de Andrade edited this page Sep 29, 2014
·
2 revisions
- Do not use tabs, use four spaces instead
- Indentation should have 4 spaces
- Break line when it has 80 characters wide
- Do not leave space at end of line
- At end of file leave a new line
- Comments and coding should be written on English
- Always
add space between declaration, operation and value - Shoud have
space after comma, but not before it - Arguments should have
no spaces between name and paretheses - Use
space between function and its parenthesis and parentheses and curly braces -
After keywords (for, while, if, functions...)should have space -
Should use NO SPACE- Inside curly braces, brackets or parentheses and its contents (between elements is imperative)
- Between names and its arguments on function calls
Good
var fileTypes = ['.html', '.js', '.py'];
var sum = a + b;
sum += 1;
var mean = sum / 3;Bad
var fileTypes = [ '.html','.js','.py' ];
var sum=a+b;
sum+=1;
var mean=sum/3;-
Should be usedeven if they are not necessary Curly braces should start on same declaration line
Good
if (obj.prop) {
obj.doSomething();
}Bad
if (obj.prop)
obj.doSomething();- Variables should
start with lower case - Always
use camelCaseon long names -
Constants should be upper casedwith underscore between words - Strings should
use single quotes -
Always insert semicolonat end of declarations
Good
var dayOfWeek = 'Monday';
var SECONDS_PER_MINUTE = 60;
var weekDays = [0, 1, 2, 3, 4, 5, 6];
var months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'oct', 'Nov', 'Dec'};Bad
var day_of_week = "Monday"
var secondsPerMinuteConstant = 60
var weekDays = [ 0,1,2,3,4,5,6 ];
var months = { 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','oct','Nov','Dec' };- Always
use camelCaseon long names - Functions should
start with lower case "Classes" should start with capitalized letter- Add space between function name and its arguments`
- Colon on object property and functions should have no space before but space after
Good
var Person = function (name) {
this.name: name,
this.setName: function (name) {
this.name = name;
},
this.getName: function (name) {
return this.name;
}
}
var voidFunction = function () {
return;
}
voidFunction();Bad
var person = function( name )
{
this.name:name,
this.setName:function ( name ) {
this.name=name;
},
this.getName: function (name) {
return this.name;
}
}
var voidFunction = function( )
{
return;
}
voidFunction ( );- Add new line before a block of code, except first line block`
var isPrime = function (num) {
var square = (int) Math.sqrt(num);
if (num < 2) {
return false;
}
for (var i = 2; i <= square; i++) {
if ((num % i) === 0) {
return false;
}
}
return true;
}Case within a switch should end with break, throw or return except on default. If is need a fall-through it should be written as an explicit comment
Good
switch (val) {
case 1:
doSomething();
//fall-through
case 2:
doSomethingElse();
break;
default:
doNothing():
}Bad
switch (val) {
case 1:
doSomething();
case 2:
doSomethingElse();
break;
default:
doNothing();
}- Are to facilitate code understanding
- Explain non-obvious decisions
- Inform function preconditions and results
- It should be succinct and direct with no obviousness
- Should stay togather with its code reference and changed when its code were refactored
- Should be in JavaDoc style
/**
* Comentário sobre funcionalidade de função
* @param val valor a ser impresso na tela
*/
var print = function (val) {
console.log(val);
}- Use tags for automatic generation
@constructor
@interface
@implements
@override
@param
@private
@protected
@public
@return
@see- Use HTML when necessary
/**
* Calcula fator baseado em três
* elementos:
* <ul>
* <li>itens enviados</li>
* <li>itens recebidos</li>
* <li>ultima data registrada</li>
* </ul>
*/