Conditions | 12 |
Paths | 3 |
Total Lines | 51 |
Lines | 51 |
Ratio | 100 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
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) throw new TypeError(str + ' too short'); |
||
92 | if (str.length > 90) throw new TypeError(str + ' too long'); |
||
93 | |||
94 | // don't allow mixed case |
||
95 | var lowered = str.toLowerCase(); |
||
96 | var uppered = str.toUpperCase(); |
||
97 | if (str !== lowered && str !== uppered) { |
||
98 | throw new Error('Mixed-case string ' + str); |
||
99 | } |
||
100 | |||
101 | str = lowered; |
||
102 | |||
103 | var split = str.lastIndexOf(SEPARATOR); |
||
104 | if (split === -1) { |
||
105 | throw new Error('No separator character for ' + str); |
||
106 | } |
||
107 | |||
108 | if (split === 0) { |
||
109 | throw new Error('Missing prefix for ' + str); |
||
110 | } |
||
111 | |||
112 | var prefix = str.slice(0, split); |
||
113 | var wordChars = str.slice(split + 1); |
||
114 | if (wordChars.length < 6) { |
||
115 | throw new Error('Data too short'); |
||
116 | } |
||
117 | |||
118 | var chk = prefixChk(prefix); |
||
119 | var words = []; |
||
120 | for (var i = 0; i < wordChars.length; ++i) { |
||
121 | var c = wordChars.charAt(i); |
||
122 | var v = ALPHABET_MAP[c]; |
||
123 | if (v === undefined) { |
||
124 | throw new Error('Unknown character ' + c); |
||
125 | } |
||
126 | |||
127 | chk = polymodStep(chk).xor(new BigInteger('' + v)); |
||
128 | // not in the checksum? |
||
129 | if (i + CSLEN >= wordChars.length) { |
||
130 | continue; |
||
131 | } |
||
132 | words.push(v); |
||
133 | } |
||
134 | |||
135 | if (chk.toString(10) !== '1') { |
||
136 | throw new Error('Invalid checksum for ' + str); |
||
137 | } |
||
138 | |||
139 | return { prefix: prefix, words: words }; |
||
140 | } |
||
141 | |||
183 |