Conditions | 20 |
Total Lines | 86 |
Code Lines | 58 |
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.LocalFileWriter.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 |
||
38 | func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) { |
||
39 | if c.Conf.FormatOneLineText { |
||
40 | path := filepath.Join(w.CurrentDir, "summary.txt") |
||
41 | text := formatOneLineSummary(rs...) |
||
42 | if err := writeFile(path, []byte(text), 0600); err != nil { |
||
43 | return xerrors.Errorf( |
||
|
|||
44 | "Failed to write to file. path: %s, err: %w", |
||
45 | path, err) |
||
46 | } |
||
47 | } |
||
48 | |||
49 | for _, r := range rs { |
||
50 | path := filepath.Join(w.CurrentDir, r.ReportFileName()) |
||
51 | |||
52 | if c.Conf.FormatJSON { |
||
53 | var p string |
||
54 | if c.Conf.Diff { |
||
55 | p = path + "_diff.json" |
||
56 | } else { |
||
57 | p = path + ".json" |
||
58 | } |
||
59 | |||
60 | var b []byte |
||
61 | if c.Conf.Debug { |
||
62 | if b, err = json.MarshalIndent(r, "", " "); err != nil { |
||
63 | return xerrors.Errorf("Failed to Marshal to JSON: %w", err) |
||
64 | } |
||
65 | } else { |
||
66 | if b, err = json.Marshal(r); err != nil { |
||
67 | return xerrors.Errorf("Failed to Marshal to JSON: %w", err) |
||
68 | } |
||
69 | } |
||
70 | if err := writeFile(p, b, 0600); err != nil { |
||
71 | return xerrors.Errorf("Failed to write JSON. path: %s, err: %w", p, err) |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if c.Conf.FormatList { |
||
76 | var p string |
||
77 | if c.Conf.Diff { |
||
78 | p = path + "_short_diff.txt" |
||
79 | } else { |
||
80 | p = path + "_short.txt" |
||
81 | } |
||
82 | |||
83 | if err := writeFile( |
||
84 | p, []byte(formatList(r)), 0600); err != nil { |
||
85 | return xerrors.Errorf( |
||
86 | "Failed to write text files. path: %s, err: %w", p, err) |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if c.Conf.FormatFullText { |
||
91 | var p string |
||
92 | if c.Conf.Diff { |
||
93 | p = path + "_full_diff.txt" |
||
94 | } else { |
||
95 | p = path + "_full.txt" |
||
96 | } |
||
97 | |||
98 | if err := writeFile( |
||
99 | p, []byte(formatFullPlainText(r)), 0600); err != nil { |
||
100 | return xerrors.Errorf( |
||
101 | "Failed to write text files. path: %s, err: %w", p, err) |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if c.Conf.FormatXML { |
||
106 | var p string |
||
107 | if c.Conf.Diff { |
||
108 | p = path + "_diff.xml" |
||
109 | } else { |
||
110 | p = path + ".xml" |
||
111 | } |
||
112 | |||
113 | var b []byte |
||
114 | if b, err = xml.Marshal(r); err != nil { |
||
115 | return xerrors.Errorf("Failed to Marshal to XML: %w", err) |
||
116 | } |
||
117 | allBytes := bytes.Join([][]byte{[]byte(xml.Header + vulsOpenTag), b, []byte(vulsCloseTag)}, []byte{}) |
||
118 | if err := writeFile(p, allBytes, 0600); err != nil { |
||
119 | return xerrors.Errorf("Failed to write XML. path: %s, err: %w", p, err) |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | return nil |
||
124 | } |
||
136 |