public
Last active

  • Download Gist
code-smells.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
// SMELLY: HTML in your JavaScript
$('#showMessage').click(function() {
$('<div>' +
'<h1>' + $('#messageTitle').val() + '</h1>' +
'<p>' + $('#messageText').val() + '</p>' +
'</div>')
.appendTo('#messageContainer')
});
 
// MINTY FRESH: Use templates instead
<script type="text/x-jquery-tmpl" id="messageTemplate">
<div>
<h1>${title}</h1>
<p>${text}</p>
</div>
</script>
 
$('#messageTemplate').template('messageTemplate');
 
$.tmpl('messageTemplate', {
title : $('#messageTitle').val(),
text : $('#messageText').val()
})
.appendTo('#messageContainer');
 
 
// SMELLY: Changing style information in JavaScript
$('p.special').click(function() {
$(this).css({
'color' : 'red',
'font-weight' : 'bold'
});
})
 
// MINTY FRESH: Keep presentation information in CSS
p.extraSpecial {
color: red;
font-weight: bold;
}
 
$('p.special').click(function() {
$(this).addClass('extraSpecial');
});
 
 
// SMELLY: Duplicating jQuery functionality
function isItemInArray(item, arr) {
var inArray = false,
len = arr.length;
 
for (var i = 0; i < len; i++) {
if (item == arr[i]) {
inArray = true;
}
}
return inArray;
}
 
// MINTY FRESH: Use jQuery!
function isItemInArray(item, arr) {
return $.inArray(item, arr) > -1;
}
 
 
// SMELLY: Repetition that jQuery lets you avoid
$('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();
});
 
 
// SMELLY: Deeply nested anonymous functions
$(document).ready(function() {
$('#enableAwesome').click(function() {
$('ul.foo li').each(function() {
var li = $(this);
 
li.data('name', li.html())
.find('a.remove').click(function(e) {
$.ajax({
url : $(this).attr('href'),
dataType : 'json',
type : 'post',
success : function(resp) {
if (resp.ok) { li.remove(); }
},
error : console.log
})
e.preventDefault();
});
})
});
});
 
// MINTY FRESH: Isolate functionality into an object with methods
var awesome = {
enableListItem : function() {
var li = $(this);
li.data('name', li.html());
},
removeListItem : function() {
var a = $(this),
li = a.closest('li');
 
awesome.removeOnServer({
url : a.attr('href'),
success : function(resp) {
if (resp.ok) { li.remove(); }
}
});
},
removeOnServer : function (config) {
var defaults = {
type : 'post',
dataType : 'json',
error : console.log
},
settings = $.extend({}, defaults, config);
 
if (!settings.url) { return; }
$.ajax(config);
}
};
 
$(document).ready(function() {
$('#enableAwesome').click(function() {
$('ul.foo li')
.each(awesomeenableListItem)
.delegate('a.remove', 'click', awesome.removeListItem);
});
});
 
 
// SMELLY: Repetitive logic blocks
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']();
}
 
 
 
 
 
// SMELLY: Passing a lot of optional arguments to a function
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);
 
// 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/>'))
}
 
doSomethingComplicated({
selector : '#foo',
words : [ 'foo', 'bar', 'baz' ],
repeat : 20
});
 
 
// SMELLY: Overtesting for truthiness
if (errorMsg != null && errorMsg.length > 0) {
// ...
}
 
// MINTY FRESH: Be as terse as you can
if (errorMsg && errorMsg.length) {
// ...
}
 
// SMELLY
if (total == null || total == "0") {
// ...
}
 
// MINTY FRESH
if (!parseInt(total, 10)) {
// ...
}
 
// 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) {
// ...
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.