| Conditions | 15 |
| Total Lines | 52 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| CRAP Score | 15.6565 |
| 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 decode.newTypeDecoder 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 decode |
||
| 171 | func newTypeDecoder(bt binn.Type) decodeFunc { |
||
| 172 | 1 | switch bt { |
|
| 173 | case binn.ListType: |
||
| 174 | 1 | return decodeList |
|
| 175 | case binn.MapType: |
||
| 176 | 1 | return decodeMap |
|
| 177 | case binn.ObjectType: |
||
| 178 | 1 | return decodeObject |
|
| 179 | case binn.True, binn.False: |
||
| 180 | 1 | return func(reader io.Reader, v interface{}) error { |
|
| 181 | 1 | valuePtr := reflect.ValueOf(v) |
|
| 182 | 1 | value := valuePtr.Elem() |
|
| 183 | |||
| 184 | 1 | if value.Kind() != reflect.Bool && value.Kind() != reflect.Interface { |
|
| 185 | 1 | return &UnknownValueError{reflect.Bool, value.Kind()} |
|
| 186 | } |
||
| 187 | |||
| 188 | 1 | if !value.CanSet() { |
|
| 189 | return ErrCantSetValue |
||
| 190 | } |
||
| 191 | |||
| 192 | 1 | value.Set(reflect.ValueOf(bt == binn.True)) |
|
| 193 | |||
| 194 | 1 | return nil |
|
| 195 | } |
||
| 196 | case binn.Null: |
||
| 197 | 1 | return func(_ io.Reader, _ interface{}) error { |
|
| 198 | 1 | return nil |
|
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | 1 | return func(reader io.Reader, v interface{}) error { |
|
| 203 | 1 | valuePtr := reflect.ValueOf(v) |
|
| 204 | 1 | value := valuePtr.Elem() |
|
| 205 | |||
| 206 | 1 | if !value.CanSet() { |
|
| 207 | return ErrCantSetValue |
||
| 208 | } |
||
| 209 | |||
| 210 | 1 | bval, err := readValue(bt, reader) |
|
| 211 | 1 | if err != nil { |
|
| 212 | return err |
||
| 213 | } |
||
| 214 | |||
| 215 | 1 | converted, err := decodeItem(value.Type(), bt, bval) |
|
| 216 | 1 | if err != nil { |
|
| 217 | return fmt.Errorf("storage can't be converted to type: %w", err) |
||
| 218 | } |
||
| 219 | |||
| 220 | 1 | value.Set(reflect.ValueOf(converted)) |
|
| 221 | |||
| 222 | 1 | return nil |
|
| 223 | } |
||
| 267 |