Conditions | 30 |
Total Lines | 126 |
Code Lines | 70 |
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 coffeescript.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 |
||
50 | function tokenBase(stream, state) { |
||
51 | // Handle scope changes |
||
52 | if (stream.sol()) { |
||
53 | if (state.scope.align === null) state.scope.align = false; |
||
54 | var scopeOffset = state.scope.offset; |
||
55 | if (stream.eatSpace()) { |
||
56 | var lineOffset = stream.indentation(); |
||
57 | if (lineOffset > scopeOffset && state.scope.type == "coffee") { |
||
58 | return "indent"; |
||
59 | } else if (lineOffset < scopeOffset) { |
||
60 | return "dedent"; |
||
61 | } |
||
62 | return null; |
||
63 | } else { |
||
64 | if (scopeOffset > 0) { |
||
65 | dedent(stream, state); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | if (stream.eatSpace()) { |
||
70 | return null; |
||
71 | } |
||
72 | |||
73 | var ch = stream.peek(); |
||
74 | |||
75 | // Handle docco title comment (single line) |
||
76 | if (stream.match("####")) { |
||
77 | stream.skipToEnd(); |
||
78 | return "comment"; |
||
79 | } |
||
80 | |||
81 | // Handle multi line comments |
||
82 | if (stream.match("###")) { |
||
83 | state.tokenize = longComment; |
||
84 | return state.tokenize(stream, state); |
||
85 | } |
||
86 | |||
87 | // Single line comment |
||
88 | if (ch === "#") { |
||
89 | stream.skipToEnd(); |
||
90 | return "comment"; |
||
91 | } |
||
92 | |||
93 | // Handle number literals |
||
94 | if (stream.match(/^-?[0-9\.]/, false)) { |
||
95 | var floatLiteral = false; |
||
96 | // Floats |
||
97 | if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i)) { |
||
98 | floatLiteral = true; |
||
99 | } |
||
100 | if (stream.match(/^-?\d+\.\d*/)) { |
||
101 | floatLiteral = true; |
||
102 | } |
||
103 | if (stream.match(/^-?\.\d+/)) { |
||
104 | floatLiteral = true; |
||
105 | } |
||
106 | |||
107 | if (floatLiteral) { |
||
108 | // prevent from getting extra . on 1.. |
||
109 | if (stream.peek() == "."){ |
||
110 | stream.backUp(1); |
||
111 | } |
||
112 | return "number"; |
||
113 | } |
||
114 | // Integers |
||
115 | var intLiteral = false; |
||
116 | // Hex |
||
117 | if (stream.match(/^-?0x[0-9a-f]+/i)) { |
||
118 | intLiteral = true; |
||
119 | } |
||
120 | // Decimal |
||
121 | if (stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/)) { |
||
122 | intLiteral = true; |
||
123 | } |
||
124 | // Zero by itself with no other piece of number. |
||
125 | if (stream.match(/^-?0(?![\dx])/i)) { |
||
126 | intLiteral = true; |
||
127 | } |
||
128 | if (intLiteral) { |
||
129 | return "number"; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // Handle strings |
||
134 | if (stream.match(stringPrefixes)) { |
||
135 | state.tokenize = tokenFactory(stream.current(), false, "string"); |
||
136 | return state.tokenize(stream, state); |
||
137 | } |
||
138 | // Handle regex literals |
||
139 | if (stream.match(regexPrefixes)) { |
||
140 | if (stream.current() != "/" || stream.match(/^.*\//, false)) { // prevent highlight of division |
||
141 | state.tokenize = tokenFactory(stream.current(), true, "string-2"); |
||
142 | return state.tokenize(stream, state); |
||
143 | } else { |
||
144 | stream.backUp(1); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | // Handle operators and delimiters |
||
149 | if (stream.match(operators) || stream.match(wordOperators)) { |
||
150 | return "operator"; |
||
151 | } |
||
152 | if (stream.match(delimiters)) { |
||
153 | return "punctuation"; |
||
154 | } |
||
155 | |||
156 | if (stream.match(constants)) { |
||
157 | return "atom"; |
||
158 | } |
||
159 | |||
160 | if (stream.match(keywords)) { |
||
161 | return "keyword"; |
||
162 | } |
||
163 | |||
164 | if (stream.match(identifiers)) { |
||
165 | return "variable"; |
||
166 | } |
||
167 | |||
168 | if (stream.match(properties)) { |
||
169 | return "property"; |
||
170 | } |
||
171 | |||
172 | // Handle non-detected items |
||
173 | stream.next(); |
||
174 | return ERRORCLASS; |
||
175 | } |
||
176 | |||
370 |
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.