Conditions | 1 |
Paths | 4 |
Total Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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:
1 | var BlocktrailSDK = require('../api_client'); |
||
78 | BlocktrailBitcoinService.prototype.getBatchUnspentOutputs = function(addresses) { |
||
79 | var self = this; |
||
80 | var deferred = q.defer(); |
||
81 | |||
82 | var batchResults = {}; //utxos mapped to addresses |
||
83 | |||
84 | async.forEachSeries(function(address, cb) { |
||
85 | var page = 1; |
||
86 | var results = null; |
||
87 | var utxos = []; |
||
88 | |||
89 | async.doWhilst(function(done) { |
||
90 | //do |
||
91 | var params = { |
||
92 | page: page, |
||
93 | limit: self.settings.paginationLimit |
||
94 | }; |
||
95 | self.client.addressUnspentOutputs(address, params).then(function(results) { |
||
96 | utxos = utxos.concat(results['data']); |
||
97 | page++; |
||
98 | done(); |
||
99 | }, function(err) { |
||
100 | console.log('error happened:', err); |
||
101 | done(err); |
||
102 | }); |
||
103 | }, function() { |
||
104 | //while |
||
105 | return results && results['data'].length > 0; |
||
106 | }, function(err) { |
||
107 | //all done |
||
108 | if (err) { |
||
109 | console.log("complete, but with errors", err.message); |
||
110 | } |
||
111 | |||
112 | //reduce the returned data into the values we're interested in, and map to the relevant addresses |
||
113 | utxos.forEach(function(utxo) { |
||
114 | var address = utxo['address']; |
||
115 | |||
116 | if (typeof batchResults[address] === "undefined") { |
||
117 | batchResults[address] = []; |
||
118 | } |
||
119 | |||
120 | batchResults[address].push({ |
||
121 | 'hash': utxo['hash'], |
||
122 | 'index': utxo['index'], |
||
123 | 'value': utxo['value'], |
||
124 | 'script_hex': utxo['script_hex'] |
||
125 | }); |
||
126 | }); |
||
127 | |||
128 | cb(); |
||
129 | }); |
||
130 | |||
131 | }, addresses, function() { |
||
132 | deferred.resolve(batchResults); |
||
133 | }); |
||
134 | |||
135 | //get unspent outputs for the current chunk of addresses - required data: hash, index, value, and script hex, |
||
136 | async.doWhilst(function(done) { |
||
137 | //do |
||
138 | var params = { |
||
139 | page: page, |
||
140 | limit: self.settings.paginationLimit |
||
141 | }; |
||
142 | self.client.batchAddressUnspentOutputs(addresses, params).then(function(results) { |
||
143 | utxos = utxos.concat(results['data']); |
||
144 | page++; |
||
145 | done(); |
||
146 | }, function(err) { |
||
147 | console.log('error happened:', err); |
||
148 | done(err); |
||
149 | }); |
||
150 | }, function() { |
||
151 | //while |
||
152 | return results && results['data'].length > 0; |
||
153 | }, function(err) { |
||
154 | //all done |
||
155 | if (err) { |
||
156 | console.log("complete, but with errors", err.message); |
||
157 | } |
||
158 | |||
159 | var batchResults = {}; //utxos mapped to addresses |
||
160 | //reduce the returned data into the values we're interested in, and map to the relevant addresses |
||
161 | utxos.forEach(function(utxo) { |
||
162 | var address = utxo['address']; |
||
163 | |||
164 | if (typeof batchResults[address] === "undefined") { |
||
165 | batchResults[address] = []; |
||
166 | } |
||
167 | |||
168 | batchResults[address].push({ |
||
169 | 'hash': utxo['hash'], |
||
170 | 'index': utxo['index'], |
||
171 | 'value': utxo['value'], |
||
172 | 'script_hex': utxo['script_hex'] |
||
173 | }); |
||
174 | }); |
||
175 | deferred.resolve(batchResults); |
||
176 | }); |
||
177 | |||
178 | return deferred.promise; |
||
179 | }; |
||
180 | |||
195 |