On Github charlieouyang / polymer-web-components
Presented by Carlos Justiniano / HC.JS / @cjus / ver: 1.1.0
--- >
/**
* transSingleExecute
* Execute a single transaction and handle commit or
* rollback, return promise
* @param transobj {object} - transaction object
* @param statements {array} - transactions statements
* @returns {promise|*|Q.promise}
*/
function transSingleExecute(transobj, statements) {
// :
return deferred.promise;
}
---
>
//Before var ageInYears = 28, daysOld; daysOld = ageInYears * 365;
//After var ageInYears = 28, daysOld, daysInYear = 365; daysOld = ageInYears * daysInYear;--- > In the above example 365 is the number of days in a year. > So it's best to describe that so it's not left in question
//Before
if (!this.loadedSubViews) { this.loadedSubViews = []; }
//After
if (!this.loadedSubViews) {
this.loadedSubViews = [];
}
---
>
//Before
if (!this.loadedSubViews)
this.loadedSubViews = [];
//After
if (!this.loadedSubViews) {
this.loadedSubViews = [];
}
---
> Using curly braces makes the code easier to refactor as you add and remove lines of code
//Before
function foo() {
var pi = 3.14159265358979323846264338327950288419716;
var radius = 12;
console.log('PI, radius: ', pi, radius);
var circumference = 2 * pi * radius;
console.log('Circumference: ', circumference);
}
//After
function foo() {
var pi;
var radius;
var circumference;
pi = 3.14159265358979323846264338327950288419716;
radius = 12;
console.log('PI, radius: ', pi, radius);
circumference = 2 * pi * radius;
console.log('Circumference: ', circumference);
}
---
> JavaScript will handle variable declarations before assignments and it will do that behind the scenes.
> So it's often best to do that in your code because it's what will happen anyway but makes for clear expectations.
//approach #1
function foo() {
var pi, radius, circumference;
pi = 3.14159265358979323846264338327950288419716;
radius = 12;
console.log('PI, radius: ', pi, radius);
circumference = 2 * pi * radius;
console.log('Circumference: ', circumference);
}
//approach #2
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference = 2 * pi * radius;
console.log('PI, radius: ', pi, radius);
console.log('Circumference: ', circumference);
}
---
> Here we see two different approaches for organizing your variables.
//Before
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference = 2 * pi * radius,
largerCircleCircumference = circumference * 10;
console.log('Larger circle circumference: ', largerCircleCircumference);
}
//After
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference,
largerCircleCircumference;
circumference = 2 * pi * radius,
largerCircleCircumference = circumference * 10;
console.log('Larger circle circumference: ', largerCircleCircumference);
}
---
> A single var statement with multiple declarations can help with variable organization but abusing this feature
with a chain of declarations should be avoided.
//Before
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference,
largerCircleCircumference;
circumference = 2 * pi * radius;
largerCircleCircumference = circumference * 10;
console.log('Larger circle circumference: ', largerCircleCircumference);
}
//adding function declarations
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference,
computeLargerCircle = function(factor) {
return circumference * 10;
};
circumference = 2 * pi * radius;
console.log('computeLargerCircle', computeLargerCircle);
console.log('Larger circle circumference: ', computeLargerCircle(10));
}
---
> In this code example, we proceed to add the computerLargerCircle function
> As you can see we can define variables and function using a single var statement
> Because JavaScript handles this from top down, our computerLargerCircle function can use the circumference variable that was defined earlier
> Let's abuse this further...
//Not great
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference = function(radius) {
return 2 * pi * radius;
},
biggerCircle = function() {
return circumference(radius) * 2;
};
console.log('Bigger circle circumference: ', biggerCircle());
}
---
> So now we continue to define other functions
> Including a biggerCircle function which in tern uses the circumference function
> Rather than to declare all of your functions this way you're better off creating an object.
//Not great
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference = function(radius) {
return 2 * pi * radius;
},
biggerCircle = function() {
return circumference(radius) * 2;
},
evenBiggerCircle = biggerCircle() * 2;
console.log('Bigger circle circumference: ', biggerCircle());
console.log('Even bigger circle circumference: ', evenBiggerCircle);
}
---
> If you can avoid it don't declare more variables after the function declarations.
> In our example above moving the evenBiggerCircle variable declaration and assignment above the biggerCircle function results in an error because biggerCircle wouldn't be defined and available until later.
function foo() {
var pi, radius, evenBiggerCircle;
function circumference(radius) {
return 2 * pi * radius;
}
function biggerCircle() {
return circumference(radius) * 2;
}
pi = 3.14159265358979323846264338327950288419716;
radius = 12;
evenBiggerCircle = biggerCircle() * 2;
console.log('Bigger circle circumference: ', biggerCircle());
console.log('Even bigger circle circumference: ', evenBiggerCircle);
}
---
> Don't forget that you don't have to assign a value to a declared variable. This can help keep your chained var
statements maintainable.
//Before
var getUserIDsToNotify = function(notifyTo, data) {
'use strict';
var userIDs = [];
var deferred = q.defer();
if (notifyTo === 'USERS') {
if (!data.user_ids) {
deferred.reject({success: false});
} else {
userIDs = data.user_ids;
deferred.resolve(userIDs);
}
} else if (notifyTo === 'GROUP') {
if (!data.group_id) {
deferred.reject({success: false});
} else {
groupLib.listGroupMembers(data.group_id)
.then(function(results) {
for (var index in results) {
userIDs.push(results[index].user_id);
}
deferred.resolve(userIDs);
})
.catch(function(error) {
deferred.reject(error);
});
}
} else if (notifyTo === 'ALL_GROUPS') {
// get all group ids and then users OR get all the users
groupLib.listAllGroupMembers()
.then(function(results) {
for (var index in results) {
userIDs.push(results[index].user_id);
}
deferred.resolve(userIDs);
})
.catch(function(error) {
deferred.reject(error);
});
}
return deferred.promise;
};
---
> Avoid large conditional blocks. Consider breaking up the body of a large conditional into functions. This can
help improve readability.
//After
function handleUsers(data, deferred) {
var userIDs = [];
if (!data.user_ids) {
deferred.reject({success: false});
} else {
userIDs = data.user_ids;
deferred.resolve(userIDs);
}
}
function handleGroup(data, deferred) {
var userIDs = [];
if (!data.group_id) {
deferred.reject({success: false});
} else {
groupLib.listGroupMembers(data.group_id)
.then(function(results) {
for (var index in results) {
userIDs.push(results[index].user_id);
}
deferred.resolve(userIDs);
})
.catch(function(error) {
deferred.reject(error);
});
}
}
function handleAllGroups(data, deferred) {
var userIDs = [];
groupLib.listAllGroupMembers()
.then(function(results) {
for (var index in results) {
userIDs.push(results[index].user_id);
}
deferred.resolve(userIDs);
})
.catch(function(error) {
deferred.reject(error);
});
}
function getUserIDsToNotify(notifyTo, data) {
'use strict';
var deferred = q.defer();
if (notifyTo === 'USERS') {
handleUsers(data, deferred);
} else if (notifyTo === 'GROUP') {
handleGroup(data, deferred);
} else if (notifyTo === 'ALL_GROUPS') {
handleAllGroups(data, deferred);
}
return deferred.promise;
};
---
> Avoid large conditional blocks. Consider breaking up the body of a large conditional into functions. This can
help improve readability.
function foo() {
var pi, radius, evenBiggerCircle;
function circumference(radius) {
return 2 * pi * radius;
}
function biggerCircle() {
return circumference(radius) * 2;
}
pi = 3.14159265358979323846264338327950288419716;
radius = 12;
evenBiggerCircle = biggerCircle() * 2;
console.log('Bigger circle circumference: ', biggerCircle());
console.log('Even bigger circle circumference: ', evenBiggerCircle);
}
---
> Move nested functions to the top of the enclosing function after the var declarations
> We saw this in an earlier code example.
> This is cleaner because as we're reading the code we can clearly see the variable followed by a list of nested functions before we look at how variables are assigned and functions invoked.
//Before
setTimeout(function() { console.log("Hello") }, 3000);
setTimeout(function() { console.log("World") }, 1000);
//After
setTimeout(function Hello() { console.log("Hello") }, 3000);
setTimeout(function World() { console.log("World") }, 1000);
---
> Anonymous functions are functions which don't have names.
> In the before example we have two functions which use a timer to output Hello World. The problem is that World will be outputted before Hello
> In a more complex example it might be difficult to understand what's going on when you look at this code in a debugger. The reason is because the functions don't have names.
> In the second example we would be able to see the debugger call stack and now which function was called when.
> Naming anonymous functions can make it easier to debug more complex code.
//Not great
function foo() {
var pi = 3.14159265358979323846264338327950288419716,
radius = 12,
circumference = function(radius) {
return 2 * pi * radius;
},
biggerCircle = function() {
return circumference(radius) * 2;
},
evenBiggerCircle = biggerCircle() * 2;
console.log('Bigger circle circumference: ', biggerCircle());
console.log('Even bigger circle circumference: ', evenBiggerCircle);
}
---
> This one is from an earlier example.
> Avoid defining functions in long var chains.
> Imagine a function which defines nested functions each of which has several dozen lines of code.
> That would be difficult to work with.
> Instead don't be afraid to pull the functions into their own stand-alone declaration.
function drawBanner(text, x, y, color, fontSize, underline, bold) {}
//Usage
drawBanner('Hello World', 100,100, 24, false, true);
//After
var helloWorldBanner;
function drawBanner(banner) {}
helloWorldBanner = {
text: 'Hellow World',
x: 100,
y: 100,
fontSize: 24,
underline: false,
bold: true;
};
drawBanner(helloWorldBanner);
---
> Consider this first example. We have a function called drawBanner which accepts seven parameters
> A usage might be something like this...
> Rather than create functions with a half dozen or more parameters it's cleaner to pass objects into functions