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