Conditions | 37 |
Total Lines | 156 |
Code Lines | 82 |
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 julia.js ➔ tokenBase 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 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
||
57 | function tokenBase(stream, state) { |
||
58 | // Handle scope changes |
||
59 | var leaving_expr = state.leaving_expr; |
||
60 | if(stream.sol()) { |
||
61 | leaving_expr = false; |
||
62 | } |
||
63 | state.leaving_expr = false; |
||
64 | if(leaving_expr) { |
||
65 | if(stream.match(/^'+/)) { |
||
66 | return 'operator'; |
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | if(stream.match(/^\.{2,3}/)) { |
||
72 | return 'operator'; |
||
73 | } |
||
74 | |||
75 | if (stream.eatSpace()) { |
||
76 | return null; |
||
77 | } |
||
78 | |||
79 | var ch = stream.peek(); |
||
80 | // Handle Comments |
||
81 | if (ch === '#') { |
||
82 | stream.skipToEnd(); |
||
83 | return 'comment'; |
||
84 | } |
||
85 | if(ch==='[') { |
||
86 | state.scopes.push("["); |
||
87 | } |
||
88 | |||
89 | if(ch==='{') { |
||
90 | state.scopes.push("{"); |
||
91 | } |
||
92 | |||
93 | var scope=cur_scope(state); |
||
94 | |||
95 | if(scope==='[' && ch===']') { |
||
96 | state.scopes.pop(); |
||
97 | state.leaving_expr=true; |
||
98 | } |
||
99 | |||
100 | if(scope==='{' && ch==='}') { |
||
101 | state.scopes.pop(); |
||
102 | state.leaving_expr=true; |
||
103 | } |
||
104 | |||
105 | if(ch===')') { |
||
106 | state.leaving_expr = true; |
||
107 | } |
||
108 | |||
109 | var match; |
||
110 | if(!in_array(state) && (match=stream.match(openers, false))) { |
||
111 | state.scopes.push(match); |
||
112 | } |
||
113 | |||
114 | if(!in_array(state) && stream.match(closers, false)) { |
||
115 | state.scopes.pop(); |
||
116 | } |
||
117 | |||
118 | if(in_array(state)) { |
||
119 | if(stream.match(/^end/)) { |
||
120 | return 'number'; |
||
121 | } |
||
122 | |||
123 | } |
||
124 | |||
125 | if(stream.match(/^=>/)) { |
||
126 | return 'operator'; |
||
127 | } |
||
128 | |||
129 | |||
130 | // Handle Number Literals |
||
131 | if (stream.match(/^[0-9\.]/, false)) { |
||
132 | var imMatcher = RegExp(/^im\b/); |
||
133 | var floatLiteral = false; |
||
134 | // Floats |
||
135 | if (stream.match(/^\d*\.(?!\.)\d+([ef][\+\-]?\d+)?/i)) { floatLiteral = true; } |
||
136 | if (stream.match(/^\d+\.(?!\.)\d*/)) { floatLiteral = true; } |
||
137 | if (stream.match(/^\.\d+/)) { floatLiteral = true; } |
||
138 | if (floatLiteral) { |
||
139 | // Float literals may be "imaginary" |
||
140 | stream.match(imMatcher); |
||
141 | state.leaving_expr = true; |
||
142 | return 'number'; |
||
143 | } |
||
144 | // Integers |
||
145 | var intLiteral = false; |
||
146 | // Hex |
||
147 | if (stream.match(/^0x[0-9a-f]+/i)) { intLiteral = true; } |
||
148 | // Binary |
||
149 | if (stream.match(/^0b[01]+/i)) { intLiteral = true; } |
||
150 | // Octal |
||
151 | if (stream.match(/^0o[0-7]+/i)) { intLiteral = true; } |
||
152 | // Decimal |
||
153 | if (stream.match(/^[1-9]\d*(e[\+\-]?\d+)?/)) { |
||
154 | intLiteral = true; |
||
155 | } |
||
156 | // Zero by itself with no other piece of number. |
||
157 | if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; } |
||
158 | if (intLiteral) { |
||
159 | // Integer literals may be "long" |
||
160 | stream.match(imMatcher); |
||
161 | state.leaving_expr = true; |
||
162 | return 'number'; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if(stream.match(/^(::)|(<:)/)) { |
||
167 | return 'operator'; |
||
168 | } |
||
169 | |||
170 | // Handle symbols |
||
171 | if(!leaving_expr && stream.match(symbol)) { |
||
172 | return 'string'; |
||
173 | } |
||
174 | |||
175 | // Handle operators and Delimiters |
||
176 | if (stream.match(operators)) { |
||
177 | return 'operator'; |
||
178 | } |
||
179 | |||
180 | |||
181 | // Handle Strings |
||
182 | if (stream.match(stringPrefixes)) { |
||
183 | state.tokenize = tokenStringFactory(stream.current()); |
||
184 | return state.tokenize(stream, state); |
||
185 | } |
||
186 | |||
187 | if (stream.match(macro)) { |
||
188 | return 'meta'; |
||
189 | } |
||
190 | |||
191 | |||
192 | if (stream.match(delimiters)) { |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | if (stream.match(keywords)) { |
||
197 | return 'keyword'; |
||
198 | } |
||
199 | |||
200 | if (stream.match(builtins)) { |
||
201 | return 'builtin'; |
||
202 | } |
||
203 | |||
204 | |||
205 | if (stream.match(identifiers)) { |
||
206 | state.leaving_expr=true; |
||
207 | return 'variable'; |
||
208 | } |
||
209 | // Handle non-detected items |
||
210 | stream.next(); |
||
211 | return ERRORCLASS; |
||
212 | } |
||
213 | |||
302 |
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.