Conditions | 14 |
Total Lines | 54 |
Code Lines | 35 |
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 haml.js ➔ html 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 |
||
41 | function html(stream, state) { |
||
42 | var ch = stream.peek(); |
||
43 | |||
44 | // handle haml declarations. All declarations that cant be handled here |
||
45 | // will be passed to html mode |
||
46 | if (state.previousToken.style == "comment" ) { |
||
47 | if (state.indented > state.previousToken.indented) { |
||
48 | stream.skipToEnd(); |
||
49 | return "commentLine"; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | if (state.startOfLine) { |
||
54 | if (ch == "!" && stream.match("!!")) { |
||
55 | stream.skipToEnd(); |
||
56 | return "tag"; |
||
57 | } else if (stream.match(/^%[\w:#\.]+=/)) { |
||
58 | state.tokenize = ruby; |
||
59 | return "hamlTag"; |
||
60 | } else if (stream.match(/^%[\w:]+/)) { |
||
61 | return "hamlTag"; |
||
62 | } else if (ch == "/" ) { |
||
63 | stream.skipToEnd(); |
||
64 | return "comment"; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if (state.startOfLine || state.previousToken.style == "hamlTag") { |
||
69 | if ( ch == "#" || ch == ".") { |
||
70 | stream.match(/[\w-#\.]*/); |
||
71 | return "hamlAttribute"; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // donot handle --> as valid ruby, make it HTML close comment instead |
||
76 | if (state.startOfLine && !stream.match("-->", false) && (ch == "=" || ch == "-" )) { |
||
77 | state.tokenize = ruby; |
||
78 | return state.tokenize(stream, state); |
||
79 | } |
||
80 | |||
81 | if (state.previousToken.style == "hamlTag" || |
||
82 | state.previousToken.style == "closeAttributeTag" || |
||
83 | state.previousToken.style == "hamlAttribute") { |
||
84 | if (ch == "(") { |
||
85 | state.tokenize = rubyInQuote(")"); |
||
86 | return state.tokenize(stream, state); |
||
87 | } else if (ch == "{") { |
||
88 | state.tokenize = rubyInQuote("}"); |
||
89 | return state.tokenize(stream, state); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return htmlMode.token(stream, state.htmlState); |
||
94 | } |
||
95 | |||
160 |
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.