|
| 1 | +# clean-code-javascript |
| 2 | +Software engineering principles, from Robert C. Martin's wonderful book *Clean Code*, adapted for JavaScript. |
| 3 | + |
| 4 | +## **Variables** |
| 5 | +### Use meaningful and pronounceable variable names |
| 6 | + |
| 7 | +**Bad:** |
| 8 | +```javascript |
| 9 | +var yyyymmdstr = moment().format('YYYY/MM/DD'); |
| 10 | +``` |
| 11 | + |
| 12 | +**Good**: |
| 13 | +```javascript |
| 14 | +var yearMonthDay = moment().format('YYYY/MM/DD'); |
| 15 | +``` |
| 16 | + |
| 17 | +### Use the same vocabulary for the same type of variable |
| 18 | + |
| 19 | +**Bad:** |
| 20 | +```javascript |
| 21 | +getUserInfo(); |
| 22 | +getClientData(); |
| 23 | +getCustomerRecord(); |
| 24 | +``` |
| 25 | + |
| 26 | +**Good**: |
| 27 | +```javascript |
| 28 | +getUser(); |
| 29 | +``` |
| 30 | + |
| 31 | +## **Functions** |
| 32 | +### Limit the amount of function parameters (2 or less) |
| 33 | +Use an object if you are finding yourself needing a lot of parameters |
| 34 | + |
| 35 | +**Bad:** |
| 36 | +```javascript |
| 37 | +function createMenu(title, body, buttonText, cancellable) { |
| 38 | + ... |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +**Good**: |
| 43 | +```javascript |
| 44 | +var menuConfig = { |
| 45 | + title: 'Foo', |
| 46 | + body: 'Bar', |
| 47 | + buttonText: 'Baz' |
| 48 | + cancellable: true |
| 49 | +} |
| 50 | + |
| 51 | +function createMenu(config) { |
| 52 | + ... |
| 53 | +} |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | +### Don't use flags as function parameters |
| 58 | +Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean. |
| 59 | + |
| 60 | +**Bad:** |
| 61 | +```javascript |
| 62 | +function createFile(name, temp) { |
| 63 | + if (temp) { |
| 64 | + fs.create('./temp/' + name); |
| 65 | + } else { |
| 66 | + fs.create(name); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +**Good**: |
| 73 | +```javascript |
| 74 | +function createTempFile(name) { |
| 75 | + fs.create('./temp/' + name); |
| 76 | +} |
| 77 | + |
| 78 | +function createFile(name) { |
| 79 | + fs.create(name); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## **Comments** |
| 84 | +### Only comment things that have business logic complexity. |
| 85 | +Comments are an apology, not a requirement. Good code *mostly* documents itself. |
| 86 | + |
| 87 | +**Bad:** |
| 88 | +```javascript |
| 89 | +function hashIt(data) { |
| 90 | + // The hash |
| 91 | + var hash = 0; |
| 92 | + |
| 93 | + // Length of string |
| 94 | + var length = data.length; |
| 95 | + |
| 96 | + // Loop through every character in data |
| 97 | + for (var i = 0; i < length; i++) { |
| 98 | + // Get character code. |
| 99 | + var char = i.charCodeAt(i); |
| 100 | + // Make the hash |
| 101 | + hash = ((hash << 5) - hash) + char; |
| 102 | + // Convert to 32-bit integer |
| 103 | + hash = hash & hash; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +**Good**: |
| 109 | +```javascript |
| 110 | + |
| 111 | +function hashIt(data) { |
| 112 | + var hash = 0; |
| 113 | + var length = data.length; |
| 114 | + |
| 115 | + for (var i = 0; i < length; i++) { |
| 116 | + var char = i.charCodeAt(i); |
| 117 | + hash = ((hash << 5) - hash) + char; |
| 118 | + |
| 119 | + // Convert to 32-bit integer |
| 120 | + hash = hash & hash; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +``` |
0 commit comments