Conditions | 35 |
Total Lines | 136 |
Code Lines | 102 |
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 logfmt.*Decoder.ScanKeyval 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 | package logfmt |
||
54 | func (dec *Decoder) ScanKeyval() bool { |
||
55 | dec.key, dec.value = nil, nil |
||
56 | if dec.err != nil { |
||
57 | return false |
||
58 | } |
||
59 | |||
60 | line := dec.s.Bytes() |
||
61 | |||
62 | // garbage |
||
63 | for p, c := range line[dec.pos:] { |
||
64 | if c > ' ' { |
||
65 | dec.pos += p |
||
66 | goto key |
||
67 | } |
||
68 | } |
||
69 | dec.pos = len(line) |
||
70 | return false |
||
71 | |||
72 | key: |
||
73 | const invalidKeyError = "invalid key" |
||
74 | |||
75 | start, multibyte := dec.pos, false |
||
76 | for p, c := range line[dec.pos:] { |
||
77 | switch { |
||
78 | case c == '=': |
||
79 | dec.pos += p |
||
80 | if dec.pos > start { |
||
81 | dec.key = line[start:dec.pos] |
||
82 | if multibyte && bytes.ContainsRune(dec.key, utf8.RuneError) { |
||
83 | dec.syntaxError(invalidKeyError) |
||
84 | return false |
||
85 | } |
||
86 | } |
||
87 | if dec.key == nil { |
||
88 | dec.unexpectedByte(c) |
||
89 | return false |
||
90 | } |
||
91 | goto equal |
||
92 | case c == '"': |
||
93 | dec.pos += p |
||
94 | dec.unexpectedByte(c) |
||
95 | return false |
||
96 | case c <= ' ': |
||
97 | dec.pos += p |
||
98 | if dec.pos > start { |
||
99 | dec.key = line[start:dec.pos] |
||
100 | if multibyte && bytes.ContainsRune(dec.key, utf8.RuneError) { |
||
101 | dec.syntaxError(invalidKeyError) |
||
102 | return false |
||
103 | } |
||
104 | } |
||
105 | return true |
||
106 | case c >= utf8.RuneSelf: |
||
107 | multibyte = true |
||
108 | } |
||
109 | } |
||
110 | dec.pos = len(line) |
||
111 | if dec.pos > start { |
||
112 | dec.key = line[start:dec.pos] |
||
113 | if multibyte && bytes.ContainsRune(dec.key, utf8.RuneError) { |
||
114 | dec.syntaxError(invalidKeyError) |
||
115 | return false |
||
116 | } |
||
117 | } |
||
118 | return true |
||
119 | |||
120 | equal: |
||
121 | dec.pos++ |
||
122 | if dec.pos >= len(line) { |
||
123 | return true |
||
124 | } |
||
125 | switch c := line[dec.pos]; { |
||
126 | case c <= ' ': |
||
127 | return true |
||
128 | case c == '"': |
||
129 | goto qvalue |
||
130 | } |
||
131 | |||
132 | // value |
||
133 | start = dec.pos |
||
134 | for p, c := range line[dec.pos:] { |
||
135 | switch { |
||
136 | case c == '=' || c == '"': |
||
137 | dec.pos += p |
||
138 | dec.unexpectedByte(c) |
||
139 | return false |
||
140 | case c <= ' ': |
||
141 | dec.pos += p |
||
142 | if dec.pos > start { |
||
143 | dec.value = line[start:dec.pos] |
||
144 | } |
||
145 | return true |
||
146 | } |
||
147 | } |
||
148 | dec.pos = len(line) |
||
149 | if dec.pos > start { |
||
150 | dec.value = line[start:dec.pos] |
||
151 | } |
||
152 | return true |
||
153 | |||
154 | qvalue: |
||
155 | const ( |
||
156 | untermQuote = "unterminated quoted value" |
||
157 | invalidQuote = "invalid quoted value" |
||
158 | ) |
||
159 | |||
160 | hasEsc, esc := false, false |
||
161 | start = dec.pos |
||
162 | for p, c := range line[dec.pos+1:] { |
||
163 | switch { |
||
164 | case esc: |
||
165 | esc = false |
||
166 | case c == '\\': |
||
167 | hasEsc, esc = true, true |
||
168 | case c == '"': |
||
169 | dec.pos += p + 2 |
||
170 | if hasEsc { |
||
171 | v, ok := unquoteBytes(line[start:dec.pos]) |
||
172 | if !ok { |
||
173 | dec.syntaxError(invalidQuote) |
||
174 | return false |
||
175 | } |
||
176 | dec.value = v |
||
177 | } else { |
||
178 | start++ |
||
179 | end := dec.pos - 1 |
||
180 | if end > start { |
||
181 | dec.value = line[start:end] |
||
182 | } |
||
183 | } |
||
184 | return true |
||
185 | } |
||
186 | } |
||
187 | dec.pos = len(line) |
||
188 | dec.syntaxError(untermQuote) |
||
189 | return false |
||
190 | } |
||
238 |