3. “... changing a software system in such a way
that it does not alter the external behavior
of the code yet improves its internal
structure.”
Martin Fowler, “Refactoring”
3
Thursday, December 2, 2010
4. Why Refactor?
Hint: Not just because it’s fun.
Thursday, December 2, 2010
5. “When you sit down and solve a
problem, that solution is
merely a rst draft.”
Stoyan Stefanov, “JavaScript Patterns”
5
Thursday, December 2, 2010
14. $('a.thinger').each(function() {
$(this).attr('href', $(this).attr('href') + '?ajax=true');
});
$('a.thinger').hide();
$('#myButton').click(function(){
$('a.thinger').show();
})
// MINTY FRESH: Use the chain and setter functions!
var thingers = $('a.thinger'), // store selection in a var
button = $('#myButton'); // just in case!
thingers.attr('href', function(idx, oldVal) {
// pass a setter function & avoid the need
// to iterate over matches
return oldVal + '?ajax=true';
}).hide();
button.click(function() {
thingers.show();
});
Repetition that jQuery lets you avoid
14
Thursday, December 2, 2010
18. // SMELLY: Overtesting for truthiness
if (errorMsg != null && errorMsg.length > 0) {
// ...
}
// MINTY FRESH: Be as terse as you can
if (errorMsg && errorMsg.length) {
// ...
}
Overtesting for truthiness
18
Thursday, December 2, 2010
19. // SMELLY
if (total == null || total == "0") {
// ...
}
// MINTY FRESH
if (!parseInt(total, 10)) {
// ...
}
19
Thursday, December 2, 2010
20. // SMELLY
if (price == null) {
// ...
} else if(discountPrice != null && price == discountPrice) {
// ...
}
// MINTY FRESH
if (!price) {
// ...
// we already know that price isn't null,
// so why test if discountPrice is? if it's
// equal to price, we know it's not null
} else if (price == discountPrice) {
// ...
}
20
Thursday, December 2, 2010
21. function isItABigNumber(num) {
if(num > 5000) {
$('#myContainer').html('<p>It was a big number</p>');
$('#myInput').val(num);
$('.thinger').hide();
} else {
$('#myContainer').html('<p>It was not a big number</p>');
$('#myInput').val('');
$('.thinger').show();
}
}
// MINTY FRESH: Only repeat what needs repeating
function isItABigNumber(num) {
var big = num > 5000;
$('#myContainer').html(big ?
'<p>It was a big number</p>' :
'<p>It was not a big number</p>');
$('#myInput').val(big ? num : '');
$('.thinger')[big ? 'hide' : 'show']();
}
Repetitive logic blocks
21
Thursday, December 2, 2010
22. function crazyConcatenation(selector, word1, word2, word3, repeat) {
var arr = [],
words = [],
joinedWords;
if (selector == null) { return; }
if (word1 == null) { return; }
if (word2 != null) { words.push(word2); }
if (word3 != null) { words.push(word3); }
if (!repeat) { repeat = 5; }
joinedWords = words.join(', ');
while (repeat--) { arr.push(joinedWords); }
$(selector).html(arr.join('<br/>'))
}
crazyConcatenation('#foo', 'Hello', null, null, 5);
Passing a lot of arguments to a function
22
Thursday, December 2, 2010
23. // MINTY FRESH: Using an object instead
function crazyConcatenation(config) {
// indicate clearly what's required
if (
!config.selector ||
!config.words ||
!config.words.length
) { return; }
var defaults = { repeat : 5 },
settings = $.extend({}, defaults, config),
joinedWords = settings.words.join(', ');
while (settings.repeat--) {
arr.push(joinedWords);
}
$(settings.selector).html(arr.join('<br/>'))
}
crazyConcatenation({
selector : '#foo',
words : [ 'foo', 'bar', 'baz' ],
repeat : 20
});
23
Thursday, December 2, 2010
24. Advanced Moves
Common patterns for improving your code
Thursday, December 2, 2010
25. “Writing to be read means writing code ... with
the idea that someone else will read it. is
fact alone will make you edit and think of better
ways to solve the problem you have at hand.”
Stoyan Stefanov, “JavaScript Patterns”
25
Thursday, December 2, 2010
26. example add the same behavior to similar
content without depending on IDs
26
Thursday, December 2, 2010
27. example cache XHR responses, and create an
API to a server-side service
27
Thursday, December 2, 2010
28. example refactor a portlet
to use the jQuery UI widget
factory
28
Thursday, December 2, 2010
29. rebeccamurphey.com
blog.rebeccamurphey.com
@rmurphey
http://github.com/rmurphey/refactor-jquery
http://pinboard.in/u:rmurphey/t:refactor/
Presented at the 2010 Rich Web Experience
29
Thursday, December 2, 2010