Conditions | 26 |
Total Lines | 97 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Tests | 49 |
CRAP Score | 27.3203 |
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.decodeItem 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 |
||
71 | func decodeItem(rt reflect.Type, btype binn.Type, bval []byte) (interface{}, error) { |
||
72 | 1 | var v interface{} |
|
73 | 1 | var err error |
|
74 | |||
75 | 1 | switch btype { |
|
76 | case binn.Null: |
||
77 | return nil, nil |
||
78 | case binn.True: |
||
79 | 1 | return true, nil |
|
80 | case binn.False: |
||
81 | 1 | return false, nil |
|
82 | case binn.Uint8Type: |
||
83 | 1 | v = Uint8(bval) |
|
84 | case binn.Uint16Type: |
||
85 | 1 | v = Uint16(bval) |
|
86 | case binn.Uint32Type: |
||
87 | 1 | v = Uint32(bval) |
|
88 | case binn.Uint64Type: |
||
89 | 1 | v = Uint64(bval) |
|
90 | case binn.Int8Type: |
||
91 | 1 | v = Int8(bval) |
|
92 | case binn.Int16Type: |
||
93 | 1 | v = Int16(bval) |
|
94 | case binn.Int32Type: |
||
95 | 1 | v = Int32(bval) |
|
96 | case binn.Int64Type: |
||
97 | 1 | v = Int64(bval) |
|
98 | case binn.Float32Type: |
||
99 | 1 | v = Float32(bval) |
|
100 | case binn.Float64Type: |
||
101 | 1 | v = Float64(bval) |
|
102 | case binn.StringType: |
||
103 | 1 | v = String(bval[:len(bval)-1]) |
|
104 | case binn.BlobType: |
||
105 | 1 | v = bval |
|
106 | case binn.ListType: |
||
107 | 1 | var l []interface{} |
|
108 | 1 | br := bytes.NewReader(bval) |
|
109 | 1 | _, wasReadLen, _ := readSize(br) |
|
110 | 1 | cnt, wasReadCnt, _ := readSize(br) |
|
111 | 1 | wasRead := wasReadLen + wasReadCnt |
|
112 | |||
113 | 1 | err = decodeListItems(br, &l, len(bval)-int(wasRead), wasRead, cnt) |
|
114 | 1 | if err != nil { |
|
115 | return nil, err |
||
116 | } |
||
117 | |||
118 | 1 | return l, nil |
|
119 | case binn.MapType: |
||
120 | 1 | br := bytes.NewReader(bval) |
|
121 | 1 | sz, rlsize, _ := readSize(br) |
|
122 | 1 | cnt, rlcnt, _ := readSize(br) |
|
123 | |||
124 | 1 | mapType := reflect.MapOf(reflect.TypeOf(int(0)), rt.Elem()) |
|
125 | 1 | ptr := reflect.New(mapType) |
|
126 | 1 | ptr.Elem().Set(reflect.MakeMap(mapType)) |
|
127 | 1 | m := ptr.Interface() |
|
128 | |||
129 | 1 | err = decodeMapItems(br, m, sz, rlsize+rlcnt, cnt) |
|
130 | 1 | if err != nil { |
|
131 | return nil, err |
||
132 | } |
||
133 | |||
134 | 1 | return reflect.ValueOf(m).Elem().Interface(), nil |
|
135 | case binn.ObjectType: |
||
136 | 1 | var obj interface{} |
|
137 | 1 | if rt.Kind() == reflect.Interface { |
|
138 | obj = map[string]interface{}{} |
||
139 | } else { |
||
140 | 1 | ptr := reflect.New(rt) |
|
141 | 1 | obj = ptr.Interface() |
|
142 | } |
||
143 | |||
144 | 1 | br := bytes.NewReader(bval) |
|
145 | |||
146 | 1 | err = decodeStorage(btype, br, &obj) |
|
147 | |||
148 | 1 | if err != nil { |
|
149 | return nil, err |
||
150 | } |
||
151 | |||
152 | 1 | return obj, nil |
|
153 | } |
||
154 | |||
155 | 1 | if rt.Kind() == reflect.Interface { |
|
156 | 1 | v, err = convertToKind(kindMapper[btype], v) |
|
157 | 1 | if err != nil { |
|
158 | return nil, err |
||
159 | } |
||
160 | } else { |
||
161 | 1 | v, err = convertToType(rt, v) |
|
162 | 1 | if err != nil { |
|
163 | return nil, err |
||
164 | } |
||
165 | } |
||
166 | |||
167 | 1 | return v, nil |
|
168 | } |
||
253 |