Conditions | 29 |
Paths | > 20000 |
Total Lines | 131 |
Code Lines | 98 |
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 language_data.js ➔ ... ➔ this.stemWord 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 | /* |
||
66 | this.stemWord = function (w) { |
||
67 | var stem; |
||
68 | var suffix; |
||
69 | var firstch; |
||
70 | var origword = w; |
||
|
|||
71 | |||
72 | if (w.length < 3) |
||
73 | return w; |
||
74 | |||
75 | var re; |
||
76 | var re2; |
||
77 | var re3; |
||
78 | var re4; |
||
79 | |||
80 | firstch = w.substr(0,1); |
||
81 | if (firstch == "y") |
||
82 | w = firstch.toUpperCase() + w.substr(1); |
||
83 | |||
84 | // Step 1a |
||
85 | re = /^(.+?)(ss|i)es$/; |
||
86 | re2 = /^(.+?)([^s])s$/; |
||
87 | |||
88 | if (re.test(w)) |
||
89 | w = w.replace(re,"$1$2"); |
||
90 | else if (re2.test(w)) |
||
91 | w = w.replace(re2,"$1$2"); |
||
92 | |||
93 | // Step 1b |
||
94 | re = /^(.+?)eed$/; |
||
95 | re2 = /^(.+?)(ed|ing)$/; |
||
96 | if (re.test(w)) { |
||
97 | var fp = re.exec(w); |
||
98 | re = new RegExp(mgr0); |
||
99 | if (re.test(fp[1])) { |
||
100 | re = /.$/; |
||
101 | w = w.replace(re,""); |
||
102 | } |
||
103 | } |
||
104 | else if (re2.test(w)) { |
||
105 | var fp = re2.exec(w); |
||
106 | stem = fp[1]; |
||
107 | re2 = new RegExp(s_v); |
||
108 | if (re2.test(stem)) { |
||
109 | w = stem; |
||
110 | re2 = /(at|bl|iz)$/; |
||
111 | re3 = new RegExp("([^aeiouylsz])\\1$"); |
||
112 | re4 = new RegExp("^" + C + v + "[^aeiouwxy]$"); |
||
113 | if (re2.test(w)) |
||
114 | w = w + "e"; |
||
115 | else if (re3.test(w)) { |
||
116 | re = /.$/; |
||
117 | w = w.replace(re,""); |
||
118 | } |
||
119 | else if (re4.test(w)) |
||
120 | w = w + "e"; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // Step 1c |
||
125 | re = /^(.+?)y$/; |
||
126 | if (re.test(w)) { |
||
127 | var fp = re.exec(w); |
||
128 | stem = fp[1]; |
||
129 | re = new RegExp(s_v); |
||
130 | if (re.test(stem)) |
||
131 | w = stem + "i"; |
||
132 | } |
||
133 | |||
134 | // Step 2 |
||
135 | re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/; |
||
136 | if (re.test(w)) { |
||
137 | var fp = re.exec(w); |
||
138 | stem = fp[1]; |
||
139 | suffix = fp[2]; |
||
140 | re = new RegExp(mgr0); |
||
141 | if (re.test(stem)) |
||
142 | w = stem + step2list[suffix]; |
||
143 | } |
||
144 | |||
145 | // Step 3 |
||
146 | re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/; |
||
147 | if (re.test(w)) { |
||
148 | var fp = re.exec(w); |
||
149 | stem = fp[1]; |
||
150 | suffix = fp[2]; |
||
151 | re = new RegExp(mgr0); |
||
152 | if (re.test(stem)) |
||
153 | w = stem + step3list[suffix]; |
||
154 | } |
||
155 | |||
156 | // Step 4 |
||
157 | re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/; |
||
158 | re2 = /^(.+?)(s|t)(ion)$/; |
||
159 | if (re.test(w)) { |
||
160 | var fp = re.exec(w); |
||
161 | stem = fp[1]; |
||
162 | re = new RegExp(mgr1); |
||
163 | if (re.test(stem)) |
||
164 | w = stem; |
||
165 | } |
||
166 | else if (re2.test(w)) { |
||
167 | var fp = re2.exec(w); |
||
168 | stem = fp[1] + fp[2]; |
||
169 | re2 = new RegExp(mgr1); |
||
170 | if (re2.test(stem)) |
||
171 | w = stem; |
||
172 | } |
||
173 | |||
174 | // Step 5 |
||
175 | re = /^(.+?)e$/; |
||
176 | if (re.test(w)) { |
||
177 | var fp = re.exec(w); |
||
178 | stem = fp[1]; |
||
179 | re = new RegExp(mgr1); |
||
180 | re2 = new RegExp(meq1); |
||
181 | re3 = new RegExp("^" + C + v + "[^aeiouwxy]$"); |
||
182 | if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) |
||
183 | w = stem; |
||
184 | } |
||
185 | re = /ll$/; |
||
186 | re2 = new RegExp(mgr1); |
||
187 | if (re.test(w) && re2.test(w)) { |
||
188 | re = /.$/; |
||
189 | w = w.replace(re,""); |
||
190 | } |
||
191 | |||
192 | // and turn initial Y back to y |
||
193 | if (firstch == "y") |
||
194 | w = firstch.toLowerCase() + w.substr(1); |
||
195 | return w; |
||
196 | } |
||
197 | } |
||
298 |