| Conditions | 12 |
| Paths | 3 |
| Total Lines | 55 |
| Lines | 55 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.js ➔ decode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | View Code Duplication | 'use strict'; |
|
| 90 | function decode(str) { |
||
| 91 | if (str.length < 8) { |
||
| 92 | throw new TypeError(str + ' too short'); |
||
| 93 | } |
||
| 94 | if (str.length > 90) { |
||
| 95 | throw new TypeError(str + ' too long'); |
||
| 96 | } |
||
| 97 | |||
| 98 | // don't allow mixed case |
||
| 99 | var lowered = str.toLowerCase(); |
||
| 100 | var uppered = str.toUpperCase(); |
||
| 101 | if (str !== lowered && str !== uppered) { |
||
| 102 | throw new Error('Mixed-case string ' + str); |
||
| 103 | } |
||
| 104 | |||
| 105 | str = lowered; |
||
| 106 | |||
| 107 | var split = str.lastIndexOf(SEPARATOR); |
||
| 108 | if (split === -1) { |
||
| 109 | throw new Error('No separator character for ' + str); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (split === 0) { |
||
| 113 | throw new Error('Missing prefix for ' + str); |
||
| 114 | } |
||
| 115 | |||
| 116 | var prefix = str.slice(0, split); |
||
| 117 | var wordChars = str.slice(split + 1); |
||
| 118 | if (wordChars.length < 6) { |
||
| 119 | throw new Error('Data too short'); |
||
| 120 | } |
||
| 121 | |||
| 122 | var chk = prefixChk(prefix); |
||
| 123 | var words = []; |
||
| 124 | for (var i = 0; i < wordChars.length; ++i) { |
||
| 125 | var c = wordChars.charAt(i); |
||
| 126 | var v = ALPHABET_MAP[c]; |
||
| 127 | if (v === undefined) { |
||
| 128 | throw new Error('Unknown character ' + c); |
||
| 129 | } |
||
| 130 | |||
| 131 | chk = polymodStep(chk).xor(new BigInteger('' + v)); |
||
| 132 | // not in the checksum? |
||
| 133 | if (i + CSLEN >= wordChars.length) { |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | words.push(v); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (chk.toString(10) !== '1') { |
||
| 140 | throw new Error('Invalid checksum for ' + str); |
||
| 141 | } |
||
| 142 | |||
| 143 | return { prefix: prefix, words: words }; |
||
| 144 | } |
||
| 145 | |||
| 186 | module.exports = { decode: decode, encode: encode, toWords: toWords, fromWords: fromWords }; |