Conditions | 10 |
Total Lines | 53 |
Code Lines | 32 |
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 cmd.scanConfigFromCmd 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 cmd |
||
14 | func scanConfigFromCmd(cmd *cobra.Command) (*scan.Config, error) { |
||
15 | c := &scan.Config{} |
||
16 | |||
17 | var err error |
||
18 | |||
19 | c.DictionaryPath = cmd.Flag(flagDictionary).Value.String() |
||
20 | |||
21 | c.HTTPMethods, err = cmd.Flags().GetStringSlice(flagHTTPMethods) |
||
22 | if err != nil { |
||
23 | return nil, errors.Wrap(err, "failed to read http methods flag") |
||
24 | } |
||
25 | |||
26 | c.Threads, err = cmd.Flags().GetInt(flagThreads) |
||
27 | if err != nil { |
||
28 | return nil, errors.Wrap(err, "failed to read threads flag") |
||
29 | } |
||
30 | |||
31 | c.TimeoutInMilliseconds, err = cmd.Flags().GetInt(flagHTTPTimeout) |
||
32 | if err != nil { |
||
33 | return nil, errors.Wrap(err, "failed to read http-timeout flag") |
||
34 | } |
||
35 | |||
36 | c.ScanDepth, err = cmd.Flags().GetInt(flagScanDepth) |
||
37 | if err != nil { |
||
38 | return nil, errors.Wrap(err, "failed to read http-timeout flag") |
||
39 | } |
||
40 | |||
41 | socks5Host := cmd.Flag(flagSocks5Host).Value.String() |
||
42 | if len(socks5Host) > 0 { |
||
43 | c.Socks5Url, err = url.Parse("socks5://" + socks5Host) |
||
44 | if err != nil { |
||
45 | return nil, errors.Wrap(err, "invalid value for "+flagSocks5Host) |
||
46 | } |
||
47 | } |
||
48 | |||
49 | c.UserAgent = cmd.Flag(flagUserAgent).Value.String() |
||
50 | |||
51 | c.UseCookieJar, err = cmd.Flags().GetBool(flagCookieJar) |
||
52 | if err != nil { |
||
53 | return nil, errors.Wrap(err, "cookie jar flag is invalid") |
||
54 | } |
||
55 | |||
56 | rawCookies, err := cmd.Flags().GetStringSlice(flagCookies) |
||
57 | if err != nil { |
||
58 | return nil, errors.Wrap(err, "failed to read cookies flag") |
||
59 | } |
||
60 | |||
61 | c.Cookies, err = rawCookiesToCookies(rawCookies) |
||
62 | if err != nil { |
||
63 | return nil, errors.Wrap(err, "failed to convert rawCookies to objects") |
||
64 | } |
||
65 | |||
66 | return c, nil |
||
67 | } |
||
90 |