Conditions | 12 |
Paths | 3 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 12 |
Changes | 1 | ||
Bugs | 0 | 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 base32.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 | 'use strict' |
||
89 | function decode (str) { |
||
90 | 37 | if (str.length < 8) { |
|
91 | 1 | throw new TypeError(str + ' too short') |
|
92 | } |
||
93 | 36 | if (str.length > 90) { |
|
94 | 1 | throw new TypeError(str + ' too long') |
|
95 | } |
||
96 | |||
97 | // don't allow mixed case |
||
98 | 35 | let lowered = str.toLowerCase() |
|
99 | 35 | let uppered = str.toUpperCase() |
|
100 | 35 | if (str !== lowered && str !== uppered) { |
|
101 | 1 | throw new Error('Mixed-case string ' + str) |
|
102 | } |
||
103 | |||
104 | 34 | str = lowered |
|
105 | |||
106 | 34 | let split = str.lastIndexOf(SEPARATOR) |
|
107 | 34 | if (split === -1) { |
|
108 | 1 | throw new Error('No separator character for ' + str) |
|
109 | } |
||
110 | |||
111 | 33 | if (split === 0) { |
|
112 | 1 | throw new Error('Missing prefix for ' + str) |
|
113 | } |
||
114 | |||
115 | 32 | let prefix = str.slice(0, split) |
|
116 | 32 | let wordChars = str.slice(split + 1) |
|
117 | 32 | if (wordChars.length < 6) { |
|
118 | 1 | throw new Error('Data too short') |
|
119 | } |
||
120 | |||
121 | 31 | let chk = prefixChk(prefix) |
|
122 | 31 | let words = [] |
|
123 | 31 | for (let i = 0; i < wordChars.length; ++i) { |
|
124 | 1228 | let c = wordChars.charAt(i) |
|
125 | 1228 | let v = ALPHABET_MAP[c] |
|
126 | 1228 | if (v === undefined) { |
|
127 | 1 | throw new Error('Unknown character ' + c) |
|
128 | } |
||
129 | |||
130 | 1227 | chk = polymodStep(chk).xor(new BigInteger('' + v)) |
|
131 | // not in the checksum? |
||
132 | 1227 | if (i + CSLEN >= wordChars.length) { |
|
133 | 240 | continue |
|
134 | } |
||
135 | 987 | words.push(v) |
|
136 | } |
||
137 | |||
138 | 30 | if (chk.toString(10) !== '1') { |
|
139 | 9 | throw new Error('Invalid checksum for ' + str) |
|
140 | } |
||
141 | |||
142 | 21 | return { prefix, words } |
|
143 | } |
||
144 | |||
186 |