Conditions | 18 |
Paths | 11 |
Total Lines | 67 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 5 | 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 stringFuncs.splitWords 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 | /** global: UB */ |
||
27 | splitWords: function(advanced = true){ |
||
28 | var text = this; |
||
29 | |||
30 | if (advanced){ |
||
31 | |||
32 | var results = []; |
||
33 | |||
34 | // find start of word |
||
35 | for (var c = 0, cl = text.length;c<cl;c++){ |
||
36 | var cc = text.charAt(c); |
||
37 | var start = c; |
||
38 | |||
39 | if (cc.isLetter()) { |
||
40 | |||
41 | // find end of word |
||
42 | for (var e = c + 1;e<cl;e++){ |
||
43 | var ec = text.charAt(e); |
||
44 | var isLastChar = (e == (cl - 1)); |
||
45 | |||
46 | if ((!ec.isLetter() && ec != ' ') || isLastChar){ |
||
47 | |||
48 | // skip if "letter [dash] letter" |
||
49 | if (!isLastChar && ec == '-' && text.charAt(e + 1).isLetter()){ |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | // add word if something found |
||
54 | var end = isLastChar?e:e-1; |
||
55 | if (end > start) { |
||
56 | |||
57 | var term = text.range(start, end).trim(); |
||
58 | if (term.exists()){ |
||
59 | |||
60 | if (splitBySpaceAlso) { |
||
|
|||
61 | term.splitWords().addArray(results); |
||
62 | } else { |
||
63 | results.push(term); |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | |||
68 | // continue at ending |
||
69 | c = e; |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | return results; |
||
76 | |||
77 | } |
||
78 | |||
79 | // SPLIT BY NEWLINE & SPACES |
||
80 | results = []; |
||
81 | var lines = text.splitLines(true, true); |
||
82 | for (var s = 0, sl = lines.length; s < sl; s++) { |
||
83 | var line = lines[s]; |
||
84 | results = line.split(" "); |
||
85 | for (var w = 0, wl = results.length; w < wl; w++) { |
||
86 | var word = results[w]; |
||
87 | if (word.length > 0) { |
||
88 | results.push(word); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | return results; |
||
93 | }, |
||
94 | |||
104 | UB.registerFuncs(String.prototype, stringFuncs); |