Conditions | 15 |
Paths | 1 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
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 sync-machine-proxy-cacheSpec.js ➔ ... ➔ describe(ꞌdata in cacheꞌ) 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 | define(function(require) { |
||
163 | describe('data in cache', function() { |
||
164 | var cacheTimeMark; |
||
165 | beforeEach(function() { |
||
166 | cacheTimeMark = Date.now(); |
||
167 | storedData[storageKey] = JSON.stringify({ |
||
168 | time: cacheTimeMark, |
||
169 | data: {foo: 'bar'} |
||
170 | }); |
||
171 | instance.ensureSync(); |
||
172 | }); |
||
173 | |||
174 | it('data from cache is set into instance', function() { |
||
175 | expect(instance.set).toHaveBeenCalledWith({foo: 'bar'}, {parse: true}); |
||
176 | }); |
||
177 | |||
178 | describe('handle load of same remote data', function() { |
||
179 | beforeEach(function(done) { |
||
180 | // simulate data sync action |
||
181 | setTimeout(function() { |
||
182 | instance.trigger('sync', instance, {foo: 'bar'}); |
||
183 | done(); |
||
184 | }); |
||
185 | }); |
||
186 | |||
187 | it('time mark of data is updated in cache', function() { |
||
188 | var data = JSON.parse(storedData[storageKey]); |
||
189 | expect(data.time).toBeGreaterThan(cacheTimeMark); |
||
190 | }); |
||
191 | |||
192 | it('no event triggered about stale data in use', function() { |
||
193 | expect(instance.trigger.calls.mostRecent().args) |
||
194 | .not.toEqual(['proxy-cache:stale-data-in-use', instance]); |
||
195 | }); |
||
196 | }); |
||
197 | |||
198 | describe('handle load of changed remote data', function() { |
||
199 | beforeEach(function(done) { |
||
200 | // simulate data sync action |
||
201 | setTimeout(function() { |
||
202 | instance.trigger('change'); // simulate change event of model |
||
203 | instance.trigger('sync', instance, {foo: 'diff'}); |
||
204 | done(); |
||
205 | }); |
||
206 | }); |
||
207 | |||
208 | it('triggered event about stale data in use', function() { |
||
209 | expect(instance.trigger.calls.mostRecent().args) |
||
210 | .toEqual(['proxy-cache:stale-data-in-use', instance]); |
||
211 | }); |
||
212 | }); |
||
213 | |||
214 | describe('handle load of updated remote data', function() { |
||
215 | beforeEach(function(done) { |
||
216 | // simulate data sync action |
||
217 | setTimeout(function() { |
||
218 | instance.trigger('update'); // simulate update event of collection |
||
219 | instance.trigger('sync', instance, [{foo: 'diff'}]); |
||
220 | done(); |
||
221 | }); |
||
222 | }); |
||
223 | |||
224 | it('triggered event about stale data in use', function() { |
||
225 | expect(instance.trigger.calls.mostRecent().args) |
||
226 | .toEqual(['proxy-cache:stale-data-in-use', instance]); |
||
227 | }); |
||
228 | }); |
||
229 | }); |
||
230 | }); |
||
232 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.