Conditions | 24 |
Total Lines | 57 |
Code Lines | 53 |
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 proto.MarshalOptions.marshalSingular 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 | // Copyright 2018 The Go Authors. All rights reserved. |
||
40 | func (o MarshalOptions) marshalSingular(b []byte, fd protoreflect.FieldDescriptor, v protoreflect.Value) ([]byte, error) { |
||
41 | switch fd.Kind() { |
||
42 | case protoreflect.BoolKind: |
||
43 | b = protowire.AppendVarint(b, protowire.EncodeBool(v.Bool())) |
||
44 | case protoreflect.EnumKind: |
||
45 | b = protowire.AppendVarint(b, uint64(v.Enum())) |
||
46 | case protoreflect.Int32Kind: |
||
47 | b = protowire.AppendVarint(b, uint64(int32(v.Int()))) |
||
48 | case protoreflect.Sint32Kind: |
||
49 | b = protowire.AppendVarint(b, protowire.EncodeZigZag(int64(int32(v.Int())))) |
||
50 | case protoreflect.Uint32Kind: |
||
51 | b = protowire.AppendVarint(b, uint64(uint32(v.Uint()))) |
||
52 | case protoreflect.Int64Kind: |
||
53 | b = protowire.AppendVarint(b, uint64(v.Int())) |
||
54 | case protoreflect.Sint64Kind: |
||
55 | b = protowire.AppendVarint(b, protowire.EncodeZigZag(v.Int())) |
||
56 | case protoreflect.Uint64Kind: |
||
57 | b = protowire.AppendVarint(b, v.Uint()) |
||
58 | case protoreflect.Sfixed32Kind: |
||
59 | b = protowire.AppendFixed32(b, uint32(v.Int())) |
||
60 | case protoreflect.Fixed32Kind: |
||
61 | b = protowire.AppendFixed32(b, uint32(v.Uint())) |
||
62 | case protoreflect.FloatKind: |
||
63 | b = protowire.AppendFixed32(b, math.Float32bits(float32(v.Float()))) |
||
64 | case protoreflect.Sfixed64Kind: |
||
65 | b = protowire.AppendFixed64(b, uint64(v.Int())) |
||
66 | case protoreflect.Fixed64Kind: |
||
67 | b = protowire.AppendFixed64(b, v.Uint()) |
||
68 | case protoreflect.DoubleKind: |
||
69 | b = protowire.AppendFixed64(b, math.Float64bits(v.Float())) |
||
70 | case protoreflect.StringKind: |
||
71 | if strs.EnforceUTF8(fd) && !utf8.ValidString(v.String()) { |
||
72 | return b, errors.InvalidUTF8(string(fd.FullName())) |
||
73 | } |
||
74 | b = protowire.AppendString(b, v.String()) |
||
75 | case protoreflect.BytesKind: |
||
76 | b = protowire.AppendBytes(b, v.Bytes()) |
||
77 | case protoreflect.MessageKind: |
||
78 | var pos int |
||
79 | var err error |
||
80 | b, pos = appendSpeculativeLength(b) |
||
81 | b, err = o.marshalMessage(b, v.Message()) |
||
82 | if err != nil { |
||
83 | return b, err |
||
84 | } |
||
85 | b = finishSpeculativeLength(b, pos) |
||
86 | case protoreflect.GroupKind: |
||
87 | var err error |
||
88 | b, err = o.marshalMessage(b, v.Message()) |
||
89 | if err != nil { |
||
90 | return b, err |
||
91 | } |
||
92 | b = protowire.AppendVarint(b, protowire.EncodeTag(fd.Number(), protowire.EndGroupType)) |
||
93 | default: |
||
94 | return b, errors.New("invalid kind %v", fd.Kind()) |
||
95 | } |
||
96 | return b, nil |
||
97 | } |
||
98 |