Conditions | 13 |
Total Lines | 58 |
Code Lines | 46 |
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 report.EMailWriter.Write 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 | /* Vuls - Vulnerability Scanner |
||
35 | func (w EMailWriter) Write(rs ...models.ScanResult) (err error) { |
||
36 | conf := config.Conf |
||
37 | var message string |
||
38 | sender := NewEMailSender() |
||
39 | |||
40 | m := map[string]int{} |
||
41 | for _, r := range rs { |
||
42 | if conf.FormatOneEMail { |
||
43 | message += formatFullPlainText(r) + "\r\n\r\n" |
||
44 | mm := r.ScannedCves.CountGroupBySeverity() |
||
45 | keys := []string{"High", "Medium", "Low", "Unknown"} |
||
46 | for _, k := range keys { |
||
47 | m[k] += mm[k] |
||
48 | } |
||
49 | } else { |
||
50 | var subject string |
||
51 | if len(r.Errors) != 0 { |
||
52 | subject = fmt.Sprintf("%s%s An error occurred while scanning", |
||
53 | conf.EMail.SubjectPrefix, r.ServerInfo()) |
||
54 | } else { |
||
55 | subject = fmt.Sprintf("%s%s %s", |
||
56 | conf.EMail.SubjectPrefix, |
||
57 | r.ServerInfo(), |
||
58 | r.ScannedCves.FormatCveSummary()) |
||
59 | } |
||
60 | if conf.FormatList { |
||
61 | message = formatList(r) |
||
62 | } else { |
||
63 | message = formatFullPlainText(r) |
||
64 | } |
||
65 | if conf.FormatOneLineText { |
||
66 | message = fmt.Sprintf("One Line Summary\r\n================\r\n%s", formatOneLineSummary(r)) |
||
67 | } |
||
68 | if err := sender.Send(subject, message); err != nil { |
||
69 | return err |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | summary := "" |
||
74 | if config.Conf.IgnoreUnscoredCves { |
||
75 | summary = fmt.Sprintf("Total: %d (High:%d Medium:%d Low:%d)", |
||
76 | m["High"]+m["Medium"]+m["Low"], m["High"], m["Medium"], m["Low"]) |
||
77 | } |
||
78 | summary = fmt.Sprintf("Total: %d (High:%d Medium:%d Low:%d ?:%d)", |
||
79 | m["High"]+m["Medium"]+m["Low"]+m["Unknown"], |
||
80 | m["High"], m["Medium"], m["Low"], m["Unknown"]) |
||
81 | origmessage := message |
||
82 | if conf.FormatOneEMail { |
||
83 | message = fmt.Sprintf("One Line Summary\r\n================\r\n%s", formatOneLineSummary(rs...)) |
||
84 | if !conf.FormatOneLineText { |
||
85 | message += fmt.Sprintf("\r\n\r\n%s", origmessage) |
||
86 | } |
||
87 | |||
88 | subject := fmt.Sprintf("%s %s", |
||
89 | conf.EMail.SubjectPrefix, summary) |
||
90 | return sender.Send(subject, message) |
||
91 | } |
||
92 | return nil |
||
93 | } |
||
165 |