Conditions | 10 |
Paths | 12 |
Total Lines | 51 |
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 Url.parseMarkers 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 | /*jslint |
||
52 | return data.map(function (dataitem) { |
||
53 | dataitem = dataitem.split(':'); |
||
54 | if (dataitem.length < 3 || dataitem.length > 6) { |
||
55 | return null; |
||
56 | } |
||
57 | |||
58 | var m = { |
||
59 | alpha: dataitem[0], |
||
60 | id: alpha2id(dataitem[0]), |
||
61 | name: null, |
||
62 | coords: null, |
||
63 | r: 0, |
||
64 | color: "" |
||
65 | }, |
||
66 | index = 1, |
||
67 | lat, |
||
68 | lon; |
||
69 | |||
70 | if (m.id < 0) { |
||
71 | return null; |
||
72 | } |
||
73 | |||
74 | lat = parseFloat(dataitem[index]); |
||
75 | lon = parseFloat(dataitem[index + 1]); |
||
76 | if (Coordinates.valid(lat, lon)) { |
||
77 | index += 2; |
||
78 | m.coords = new google.maps.LatLng(lat, lon); |
||
79 | } else { |
||
80 | m.coords = Coordinates.fromString(dataitem[index]); |
||
81 | index += 1; |
||
82 | } |
||
83 | if (!m.coords) { |
||
84 | return null; |
||
85 | } |
||
86 | |||
87 | m.r = App.repairRadius(parseFloat(dataitem[index]), 0); |
||
88 | index = index + 1; |
||
89 | |||
90 | if (index < dataitem.length && |
||
91 | (/^([a-zA-Z0-9\-_]*)$/).test(dataitem[index])) { |
||
92 | m.name = dataitem[index]; |
||
93 | } |
||
94 | |||
95 | index = index + 1; |
||
96 | if (index < dataitem.length && |
||
97 | (/^([a-fA-F0-9]{6})$/).test(dataitem[index])) { |
||
98 | m.color = dataitem[index]; |
||
99 | } |
||
100 | |||
101 | return m; |
||
102 | }).filter(function (thing) { |
||
103 | return thing !== null; |
||
154 |