Conditions | 18 |
Total Lines | 85 |
Code Lines | 56 |
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 pinpointemail.SendEmailRequest.ToInput 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 pinpointemail |
||
53 | func (r SendEmailRequest) ToInput() *SDK.SendEmailInput { |
||
54 | in := &SDK.SendEmailInput{} |
||
55 | switch { |
||
56 | case len(r.To) != 0, |
||
57 | len(r.Cc) != 0, |
||
58 | len(r.Bcc) != 0: |
||
59 | in.Destination = &SDK.Destination{ |
||
60 | ToAddresses: r.To, |
||
61 | CcAddresses: r.Cc, |
||
62 | BccAddresses: r.Bcc, |
||
63 | } |
||
64 | } |
||
65 | if r.From != "" { |
||
66 | in.FromEmailAddress = pointers.String(r.From) |
||
67 | } |
||
68 | |||
69 | if len(r.RawMessageData) != 0 { |
||
70 | in.Content = &SDK.EmailContent{ |
||
71 | Raw: &SDK.RawMessage{ |
||
72 | Data: r.RawMessageData, |
||
73 | }, |
||
74 | } |
||
75 | } |
||
76 | |||
77 | switch { |
||
78 | case r.Subject != "", |
||
79 | r.Body != "": |
||
80 | if in.Content == nil { |
||
81 | in.Content = &SDK.EmailContent{} |
||
82 | } |
||
83 | in.Content.Simple = &SDK.Message{} |
||
84 | } |
||
85 | |||
86 | if r.Subject != "" { |
||
87 | content := SDK.Content{ |
||
88 | Data: pointers.String(r.Subject), |
||
89 | } |
||
90 | if r.SubjectCharset != "" { |
||
91 | content.Charset = pointers.String(r.SubjectCharset) |
||
92 | } |
||
93 | in.Content.Simple.Subject = &content |
||
94 | } |
||
95 | |||
96 | if r.Body != "" { |
||
97 | content := SDK.Content{ |
||
98 | Data: pointers.String(r.Body), |
||
99 | } |
||
100 | if r.BodyCharset != "" { |
||
101 | content.Charset = pointers.String(r.BodyCharset) |
||
102 | } |
||
103 | |||
104 | body := SDK.Body{} |
||
105 | switch { |
||
106 | case r.UseHTML: |
||
107 | body.Html = &content |
||
108 | default: |
||
109 | body.Text = &content |
||
110 | } |
||
111 | in.Content.Simple.Body = &body |
||
112 | } |
||
113 | |||
114 | if r.TemplateARN != "" { |
||
115 | template := SDK.Template{ |
||
116 | TemplateArn: pointers.String(r.TemplateARN), |
||
117 | } |
||
118 | if r.TemplateData != "" { |
||
119 | template.TemplateData = pointers.String(r.TemplateData) |
||
120 | } |
||
121 | in.Content.Template = &template |
||
122 | } |
||
123 | |||
124 | if r.ConfigurationSetName != "" { |
||
125 | in.ConfigurationSetName = pointers.String(r.ConfigurationSetName) |
||
126 | } |
||
127 | if len(r.EmailTags) != 0 { |
||
128 | list := make([]SDK.MessageTag, len(r.EmailTags)) |
||
129 | for i, v := range r.EmailTags { |
||
130 | list[i] = v.ToEmailTag() |
||
131 | } |
||
132 | } |
||
133 | if r.FeedbackForwardingEmailAddress != "" { |
||
134 | in.FeedbackForwardingEmailAddress = pointers.String(r.FeedbackForwardingEmailAddress) |
||
135 | } |
||
136 | in.ReplyToAddresses = r.ReplyToAddresses |
||
137 | return in |
||
138 | } |
||
155 |