Conditions | 20 |
Total Lines | 65 |
Code Lines | 45 |
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 mysql.*mysqlField.scanType 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 | // Go MySQL Driver - A MySQL-Driver for Go's database/sql package |
||
128 | func (mf *mysqlField) scanType() reflect.Type { |
||
129 | switch mf.fieldType { |
||
130 | case fieldTypeTiny: |
||
131 | if mf.flags&flagNotNULL != 0 { |
||
132 | if mf.flags&flagUnsigned != 0 { |
||
133 | return scanTypeUint8 |
||
134 | } |
||
135 | return scanTypeInt8 |
||
136 | } |
||
137 | return scanTypeNullInt |
||
138 | |||
139 | case fieldTypeShort, fieldTypeYear: |
||
140 | if mf.flags&flagNotNULL != 0 { |
||
141 | if mf.flags&flagUnsigned != 0 { |
||
142 | return scanTypeUint16 |
||
143 | } |
||
144 | return scanTypeInt16 |
||
145 | } |
||
146 | return scanTypeNullInt |
||
147 | |||
148 | case fieldTypeInt24, fieldTypeLong: |
||
149 | if mf.flags&flagNotNULL != 0 { |
||
150 | if mf.flags&flagUnsigned != 0 { |
||
151 | return scanTypeUint32 |
||
152 | } |
||
153 | return scanTypeInt32 |
||
154 | } |
||
155 | return scanTypeNullInt |
||
156 | |||
157 | case fieldTypeLongLong: |
||
158 | if mf.flags&flagNotNULL != 0 { |
||
159 | if mf.flags&flagUnsigned != 0 { |
||
160 | return scanTypeUint64 |
||
161 | } |
||
162 | return scanTypeInt64 |
||
163 | } |
||
164 | return scanTypeNullInt |
||
165 | |||
166 | case fieldTypeFloat: |
||
167 | if mf.flags&flagNotNULL != 0 { |
||
168 | return scanTypeFloat32 |
||
169 | } |
||
170 | return scanTypeNullFloat |
||
171 | |||
172 | case fieldTypeDouble: |
||
173 | if mf.flags&flagNotNULL != 0 { |
||
174 | return scanTypeFloat64 |
||
175 | } |
||
176 | return scanTypeNullFloat |
||
177 | |||
178 | case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar, |
||
179 | fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB, |
||
180 | fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB, |
||
181 | fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON, |
||
182 | fieldTypeTime: |
||
183 | return scanTypeRawBytes |
||
184 | |||
185 | case fieldTypeDate, fieldTypeNewDate, |
||
186 | fieldTypeTimestamp, fieldTypeDateTime: |
||
187 | // NullTime is always returned for more consistent behavior as it can |
||
188 | // handle both cases of parseTime regardless if the field is nullable. |
||
189 | return scanTypeNullTime |
||
190 | |||
191 | default: |
||
192 | return scanTypeUnknown |
||
193 | } |
||
195 |