Conditions | 10 |
Total Lines | 23 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like internal.IsAssigned 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 internal |
||
12 | func IsAssigned(a interface{}) bool { |
||
13 | if a == nil { |
||
14 | return false |
||
15 | } |
||
16 | val := reflect.ValueOf(a) |
||
17 | switch val.Kind() { |
||
18 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
||
19 | return (val.Int()) != 0 |
||
20 | case reflect.Float32, reflect.Float64: |
||
21 | return (val.Float()) != 0.0 |
||
22 | case reflect.String: |
||
23 | return (fmt.Sprintf("%s", a)) != "" |
||
24 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
||
25 | return (val.Uint()) != 0 |
||
26 | case reflect.Bool: |
||
27 | return (val.Bool()) |
||
28 | case reflect.Chan, reflect.Func, reflect.Struct, reflect.UnsafePointer: |
||
29 | tt := reflect.Zero(val.Type()) |
||
30 | return !reflect.DeepEqual(val, tt) |
||
31 | case reflect.Array, reflect.Slice: |
||
32 | return val.Len() > 0 |
||
33 | default: |
||
34 | return false |
||
35 | } |
||
37 |