Conditions | 8 |
Total Lines | 54 |
Code Lines | 43 |
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:
1 | /* Vuls - Vulnerability Scanner |
||
109 | func (e *emailSender) Send(subject, body string) (err error) { |
||
110 | emailConf := e.conf |
||
111 | to := strings.Join(emailConf.To[:], ", ") |
||
112 | cc := strings.Join(emailConf.Cc[:], ", ") |
||
113 | mailAddresses := append(emailConf.To, emailConf.Cc...) |
||
114 | if _, err := mail.ParseAddressList(strings.Join(mailAddresses[:], ", ")); err != nil { |
||
115 | return fmt.Errorf("Failed to parse email addresses: %s", err) |
||
116 | } |
||
117 | |||
118 | headers := make(map[string]string) |
||
119 | headers["From"] = emailConf.From |
||
120 | headers["To"] = to |
||
121 | headers["Cc"] = cc |
||
122 | headers["Subject"] = subject |
||
123 | headers["Date"] = time.Now().Format(time.RFC1123Z) |
||
124 | headers["Content-Type"] = "text/plain; charset=utf-8" |
||
125 | |||
126 | var header string |
||
127 | for k, v := range headers { |
||
128 | header += fmt.Sprintf("%s: %s\r\n", k, v) |
||
129 | } |
||
130 | message := fmt.Sprintf("%s\r\n%s", header, body) |
||
131 | |||
132 | smtpServer := net.JoinHostPort(emailConf.SMTPAddr, emailConf.SMTPPort) |
||
133 | |||
134 | if emailConf.User != "" && emailConf.Password != "" { |
||
135 | err = e.send( |
||
136 | smtpServer, |
||
137 | smtp.PlainAuth( |
||
138 | "", |
||
139 | emailConf.User, |
||
140 | emailConf.Password, |
||
141 | emailConf.SMTPAddr, |
||
142 | ), |
||
143 | emailConf.From, |
||
144 | mailAddresses, |
||
145 | []byte(message), |
||
146 | ) |
||
147 | if err != nil { |
||
148 | return fmt.Errorf("Failed to send emails: %s", err) |
||
149 | } |
||
150 | return nil |
||
151 | } else { |
||
|
|||
152 | err = e.send( |
||
153 | smtpServer, |
||
154 | nil, |
||
155 | emailConf.From, |
||
156 | mailAddresses, |
||
157 | []byte(message), |
||
158 | ) |
||
159 | if err != nil { |
||
160 | return fmt.Errorf("Failed to send emails: %s", err) |
||
161 | } |
||
162 | return nil |
||
163 | } |
||
170 |