On Github timruffles / javascript-code-quality
go a@(Internal al ar abyte abits) ak b@(Internal bl br bbyte bbits) bk
| (dbyte, dbits) < min (abyte, abits) (bbyte, bbits) = fork a ak b bk
| otherwise =
case compare (abyte, abits) (bbyte, bbits) of
LT -> splitA a ak b bk
GT -> splitB a ak b bk
EQ -> link a (go al ak bl bk) (go ar (minKey ar) br (minKey br))
@garybernhardt For example: Looking at this code is a great way to demonstrate how terrible it is to treat AR as a DAO: http://t.co/NkxWODIb
— DHH (@dhh) December 17, 2012A sentence should contain no unnecessary words, a paragraph no unnecessary sentences.
William Strunk Jr. in Elements of Style
function email(string) {
if(emailRe.test(string)) {
return true;
} else {
return false;
}
}
$("form").submit(function(el) {
if($(el).find("[name=age]").val() > 18) {
}
})
from Latin simplus. Originally referred to a medicine made from one constituent
function main() {
// ... 5000 lines
}
open("tcp://google.com")
open("tcp://some-socket.sock")
open("/dev/tty18")
open("path/to/some-file")
function Account() {
}
Account.prototype.transfer = function(anotherAccount) {
// very little work per method
return new AccountTransfer(this,anotherAccount).run();
}
<h2>{{ framework.name }} is HTML made awesome!</h2>
function sprintf(format,...args) {
// completely depends on input
}
function UserView() {
// depends on everything
this.el = $("#user");
this.user = $.get("/users/" + window.user.id);
}
if(x) {
} else {
}
if(y) {
} else {
}
if(x) {
} else {
}
if(y) {
} else {
}
$digest: function() {
var watch, value, last,
watchers,
asyncQueue = this.$$asyncQueue,
postDigestQueue = this.$$postDigestQueue,
length,
dirty, ttl = TTL,
next, current, target = this,
watchLog = [],
logIdx, logMsg, asyncTask;
// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $broadcast
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
while(current !== target && !(next = current.$$nextSibling)) {
current = current.$parent;
}
}
} while ((current = next));
// global variable - regardless of the 'module' in there
require("models/user.js").userCount += 1;
// exactly the same
(global || window).require("models/user.js").userCount += 1;
;(function(undefined) {
window.something = something;
function something(n,opts) {
var user = opts && opts.user;
var n = run == null ? 0 : n;
var names = [].slice.call(arguments,2);
for(var p in x) {
if(x.hasOwnProperty(p)) // ...
}
}
})();
If you’re willing to restrict the flexibility of your approach, you can almost always do something better
John Carmack
var Public = (function() {
var cantSeeMe = //;
function Public() {
privateA();
}
function privateA() {
cantSeeMe;
}
return Public;
})();
var Public = (function() {
function Public() {
privateA();
}
Public._canSeeMe = //;
Public._privateA = function () {
this._canSeeMe;
}
return Public;
})();
function sprintf(str) {
var params = [].slice.call(arguments,1)
var types = {
"%s": function(x) { return x + "" }
}
var matches = 0
return str.replace(/(%[is])/g,function(match) {
return types[match](params[matches++])
})
}
it("formats correctly",function() {
assert.equal("MU!",sprintf("%s","MU!"));
})
'generateResetToken': function (req, res) {
var email = req.body.email;
api.users.generateResetToken(email).then(function (token) {
var siteLink = '<a href="' + config().url + '">' + config().url + '</a>',
resetUrl = config().url.replace(/\/$/, '') + '/ghost/reset/' + token + '/',
resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>',
message = {
to: email,
subject: 'Reset Password',
html: '<p><strong>Hello!</strong></p>' +
'<p>A request has been made to reset the password on the site ' + siteLink + '.</p>' +
'<p>Please follow the link below to reset your password:<br><br>' + resetLink + '</p>' +
'<p>Ghost</p>'
};
return mailer.send(message);
}).then(function success() {
var notification = {
type: 'success',
message: 'Check your email for further instructions',
status: 'passive',
id: 'successresetpw'
};
return api.notifications.add(notification).then(function () {
res.json(200, {redirect: config.paths().webroot + '/ghost/signin/'});
});
}, function failure(error) {
// TODO: This is kind of sketchy, depends on magic string error.message from Bookshelf.
// TODO: It's debatable whether we want to just tell the user we sent the email in this case or not, we are giving away sensitive info here.
if (error && error.message === 'EmptyResponse') {
error.message = "Invalid email address";
}
res.json(401, {error: error.message});
});
},