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