Conditions | 5 |
Paths | 10 |
Total Lines | 57 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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:
1 | function domReady(fn) { |
||
2 | 'use strict'; |
||
3 | |||
4 | var ready = false; |
||
5 | |||
6 | function detach() { |
||
7 | if (document.addEventListener) { |
||
8 | document.removeEventListener('DOMContentLoaded', completed); |
||
9 | window.removeEventListener('load', completed); |
||
10 | } else { |
||
11 | document.detachEvent('onreadystatechange', completed); |
||
12 | window.detachEvent('onload', completed); |
||
13 | } |
||
14 | } |
||
15 | |||
16 | function completed() { |
||
17 | if (!ready && (document.addEventListener || event.type === 'load' || document.readyState === 'complete')) { |
||
|
|||
18 | ready = true; |
||
19 | detach(); |
||
20 | fn(); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | if (document.readyState === 'complete') { |
||
25 | fn(); |
||
26 | } else if (document.addEventListener) { |
||
27 | document.addEventListener('DOMContentLoaded', completed); |
||
28 | window.addEventListener('load', completed); |
||
29 | } else { |
||
30 | document.attachEvent('onreadystatechange', completed); |
||
31 | window.attachEvent('onload', completed); |
||
32 | |||
33 | var top = false; |
||
34 | |||
35 | try { |
||
36 | top = window.frameElement == null && document.documentElement; |
||
37 | } catch (e) {} |
||
38 | |||
39 | if (top && top.doScroll) { |
||
40 | (function scrollCheck() { |
||
41 | if (ready) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | try { |
||
46 | top.doScroll('left'); |
||
47 | } catch (e) { |
||
48 | return setTimeout(scrollCheck, 50); |
||
49 | } |
||
50 | |||
51 | ready = true; |
||
52 | detach(); |
||
53 | fn(); |
||
54 | })(); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 |
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.