Conditions | 19 |
Total Lines | 98 |
Code Lines | 76 |
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 tiki.js ➔ inText 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 inText(stream, state) { |
||
42 | function chain(parser) { |
||
43 | state.tokenize = parser; |
||
44 | return parser(stream, state); |
||
45 | } |
||
46 | |||
47 | var sol = stream.sol(); |
||
48 | var ch = stream.next(); |
||
49 | |||
50 | //non start of line |
||
51 | switch (ch) { //switch is generally much faster than if, so it is used here |
||
52 | case "{": //plugin |
||
53 | stream.eat("/"); |
||
54 | stream.eatSpace(); |
||
55 | var tagName = ""; |
||
56 | var c; |
||
57 | while ((c = stream.eat(/[^\s\u00a0=\"\'\/?(}]/))) tagName += c; |
||
58 | state.tokenize = inPlugin; |
||
59 | return "tag"; |
||
60 | break; |
||
61 | case "_": //bold |
||
62 | if (stream.eat("_")) { |
||
63 | return chain(inBlock("strong", "__", inText)); |
||
64 | } |
||
65 | break; |
||
66 | case "'": //italics |
||
67 | if (stream.eat("'")) { |
||
68 | // Italic text |
||
69 | return chain(inBlock("em", "''", inText)); |
||
70 | } |
||
71 | break; |
||
72 | case "(":// Wiki Link |
||
73 | if (stream.eat("(")) { |
||
74 | return chain(inBlock("variable-2", "))", inText)); |
||
75 | } |
||
76 | break; |
||
77 | case "[":// Weblink |
||
78 | return chain(inBlock("variable-3", "]", inText)); |
||
79 | break; |
||
80 | case "|": //table |
||
81 | if (stream.eat("|")) { |
||
82 | return chain(inBlock("comment", "||")); |
||
83 | } |
||
84 | break; |
||
85 | case "-": |
||
86 | if (stream.eat("=")) {//titleBar |
||
87 | return chain(inBlock("header string", "=-", inText)); |
||
88 | } else if (stream.eat("-")) {//deleted |
||
89 | return chain(inBlock("error tw-deleted", "--", inText)); |
||
90 | } |
||
91 | break; |
||
92 | case "=": //underline |
||
93 | if (stream.match("==")) { |
||
94 | return chain(inBlock("tw-underline", "===", inText)); |
||
95 | } |
||
96 | break; |
||
97 | case ":": |
||
98 | if (stream.eat(":")) { |
||
99 | return chain(inBlock("comment", "::")); |
||
100 | } |
||
101 | break; |
||
102 | case "^": //box |
||
103 | return chain(inBlock("tw-box", "^")); |
||
104 | break; |
||
105 | case "~": //np |
||
106 | if (stream.match("np~")) { |
||
107 | return chain(inBlock("meta", "~/np~")); |
||
108 | } |
||
109 | break; |
||
110 | } |
||
111 | |||
112 | //start of line types |
||
113 | if (sol) { |
||
114 | switch (ch) { |
||
115 | case "!": //header at start of line |
||
116 | if (stream.match('!!!!!')) { |
||
117 | return chain(inLine("header string")); |
||
118 | } else if (stream.match('!!!!')) { |
||
119 | return chain(inLine("header string")); |
||
120 | } else if (stream.match('!!!')) { |
||
121 | return chain(inLine("header string")); |
||
122 | } else if (stream.match('!!')) { |
||
123 | return chain(inLine("header string")); |
||
124 | } else { |
||
125 | return chain(inLine("header string")); |
||
126 | } |
||
127 | break; |
||
128 | case "*": //unordered list line item, or <li /> at start of line |
||
129 | case "#": //ordered list line item, or <li /> at start of line |
||
130 | case "+": //ordered list line item, or <li /> at start of line |
||
131 | return chain(inLine("tw-listitem bracket")); |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | //stream.eatWhile(/[&{]/); was eating up plugins, turned off to act less like html and more like tiki |
||
137 | return null; |
||
138 | } |
||
139 | |||
324 |
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.