Conditions | 16 |
Total Lines | 59 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 elk_relativeTime.js ➔ updateRelativeTime 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 | /*! |
||
12 | function updateRelativeTime () |
||
13 | { |
||
14 | const timeElements = document.querySelectorAll('time'); |
||
15 | |||
16 | let relative_time_refresh = 3600000; |
||
17 | |||
18 | timeElements.forEach(function(timeElement) { |
||
19 | let oRelativeTime = new relativeTime(timeElement.getAttribute('data-timestamp') * 1000, oRttime.referenceTime), |
||
20 | time_text = ''; |
||
21 | |||
22 | if (oRelativeTime.seconds()) |
||
23 | { |
||
24 | timeElement.textContent = oRttime.now; |
||
25 | relative_time_refresh = Math.min(relative_time_refresh, 10000); |
||
26 | } |
||
27 | else if (oRelativeTime.minutes()) |
||
28 | { |
||
29 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.minutes : oRttime.minute; |
||
30 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
31 | relative_time_refresh = Math.min(relative_time_refresh, 60000); |
||
32 | } |
||
33 | else if (oRelativeTime.hours()) |
||
34 | { |
||
35 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.hours : oRttime.hour; |
||
36 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
37 | relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
||
38 | } |
||
39 | else if (oRelativeTime.days()) |
||
40 | { |
||
41 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.days : oRttime.day; |
||
42 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
43 | relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
||
44 | } |
||
45 | else if (oRelativeTime.weeks()) |
||
46 | { |
||
47 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.weeks : oRttime.week; |
||
48 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
49 | relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
||
50 | } |
||
51 | else if (oRelativeTime.months()) |
||
52 | { |
||
53 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.months : oRttime.month; |
||
54 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
55 | relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
||
56 | } |
||
57 | else if (oRelativeTime.years()) |
||
58 | { |
||
59 | time_text = oRelativeTime.deltaTime > 1 ? oRttime.years : oRttime.year; |
||
60 | timeElement.textContent = time_text.replace('%s', oRelativeTime.deltaTime); |
||
61 | relative_time_refresh = Math.min(relative_time_refresh, 3600000); |
||
62 | } |
||
63 | }); |
||
64 | |||
65 | oRttime.referenceTime += relative_time_refresh; |
||
66 | |||
67 | setTimeout(function() { |
||
68 | updateRelativeTime(); |
||
69 | }, relative_time_refresh); |
||
70 | } |
||
71 | |||
193 |
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.