Shorthanders

Dienstag, 8. April 2014
As I often see many developer write way to much code even if the do not have to. Here are my tipps for you to safe lines of coding :)

//////////////////////////////////////////////
// 1. variable declaration
//////////////////////////////////////////////
// Longhand
var var1;
var var2 = 'Yes';
var var3 = 3;
// Shorthand
var var1,
var2 = 'Yes',
var3 = 3;
// Shortenerhand :D
var var1, var2 = 'Yes', var3 = 3;
//////////////////////////////////////////////
// 2. if true ... else
//////////////////////////////////////////////
// Longhand
var many;
if (x > 10) {
many = 'Yes';
}
else {
many = 'No';
}
// Shorthand
var many = (x > 10) ? 'Yes' : 'No';
//////////////////////////////////////////////
// 3. Check !null, !undefined, !empty('')
//////////////////////////////////////////////
// Longhand
if (var1 !== null || var1 !== undefined || var1 !== '') {
// something
}
// Shorthand
if (var1) {
// something
}
//////////////////////////////////////////////
// 4. Assign if !null, !undefined, !empty('')
//////////////////////////////////////////////
// Longhand
if (var1 !== null || var1 !== undefined || var1 !== '') {
var var2 = var1;
}
// Shorthand
var var2 = var1 || '';
//////////////////////////////////////////////
// 5. assignment operators
//////////////////////////////////////////////
// Longhand
x = x + y;
x = x - y;
x = x * y;
x = x / y;
x = x % y;
// Shorthand
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
view raw Shorthanders.js hosted with ❤ by GitHub

Keine Kommentare:

Kommentar veröffentlichen