Conditions | 27 |
Total Lines | 98 |
Code Lines | 59 |
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 dynamodb.XConditions.Build 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 dynamodb |
||
31 | func (x XConditions) Build() (expression.Expression, error) { |
||
|
|||
32 | b := expression.NewBuilder() |
||
33 | |||
34 | if len(x.KeyConditions) != 0 { |
||
35 | kc, err := x.KeyConditions[0].KeyCondition() |
||
36 | if err != nil { |
||
37 | return expression.Expression{}, err |
||
38 | } |
||
39 | // for multiple key conditions |
||
40 | if len(x.KeyConditions) > 1 { |
||
41 | for _, v := range x.KeyConditions[1:] { |
||
42 | kc2, err := v.KeyCondition() |
||
43 | if err != nil { |
||
44 | return expression.Expression{}, err |
||
45 | } |
||
46 | kc = kc.And(kc2) |
||
47 | } |
||
48 | } |
||
49 | b = b.WithKeyCondition(kc) |
||
50 | } |
||
51 | |||
52 | if len(x.Conditions) != 0 { |
||
53 | cond, err := x.Conditions[0].Condition() |
||
54 | if err != nil { |
||
55 | return expression.Expression{}, err |
||
56 | } |
||
57 | |||
58 | // for multiple conditions |
||
59 | if len(x.Conditions) > 1 { |
||
60 | for _, v := range x.Conditions[1:] { |
||
61 | cond2, err := v.Condition() |
||
62 | if err != nil { |
||
63 | return expression.Expression{}, err |
||
64 | } |
||
65 | if v.IsNOT { |
||
66 | cond2 = cond2.Not() |
||
67 | } |
||
68 | |||
69 | switch { |
||
70 | case v.IsOR: |
||
71 | cond = cond.Or(cond2) |
||
72 | default: |
||
73 | cond = cond.And(cond2) |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | b = b.WithCondition(cond) |
||
78 | } |
||
79 | |||
80 | if len(x.Filters) != 0 { |
||
81 | filt, err := x.Filters[0].Condition() |
||
82 | if err != nil { |
||
83 | return expression.Expression{}, err |
||
84 | } |
||
85 | |||
86 | // for multiple conditions |
||
87 | if len(x.Filters) > 1 { |
||
88 | for _, v := range x.Filters[1:] { |
||
89 | filt2, err := v.Condition() |
||
90 | if err != nil { |
||
91 | return expression.Expression{}, err |
||
92 | } |
||
93 | if v.IsNOT { |
||
94 | filt2 = filt2.Not() |
||
95 | } |
||
96 | |||
97 | switch { |
||
98 | case v.IsOR: |
||
99 | filt = filt.Or(filt2) |
||
100 | default: |
||
101 | filt = filt.And(filt2) |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | b = b.WithFilter(filt) |
||
106 | } |
||
107 | |||
108 | if len(x.Updates) != 0 { |
||
109 | cond := x.Updates[0].NewCondition() |
||
110 | |||
111 | // for multiple conditions |
||
112 | if len(x.Updates) > 1 { |
||
113 | for _, v := range x.Updates[1:] { |
||
114 | cond = v.updateCondition(cond) |
||
115 | } |
||
116 | } |
||
117 | b = b.WithUpdate(cond) |
||
118 | } |
||
119 | |||
120 | if len(x.Projections) != 0 { |
||
121 | list := make([]expression.NameBuilder, len(x.Projections)) |
||
122 | for i, v := range x.Projections { |
||
123 | list[i] = expression.Name(v) |
||
124 | } |
||
125 | b = b.WithProjection(expression.ProjectionBuilder{}.AddNames(list...)) |
||
126 | } |
||
127 | |||
128 | return b.Build() |
||
129 | } |
||
272 |