| Conditions | 13 |
| Total Lines | 57 |
| Code Lines | 38 |
| 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.scanHandler 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 |
||
| 40 | func scanHandler(cmd *cobra.Command, args []string) { |
||
| 41 | log.Log("Scanning", util.Abs(scanFlags.Directory())) |
||
| 42 | |||
| 43 | var files = scan.MustDir(scanFlags.Directory(), config.App.Extensions()) |
||
| 44 | poolSize, chunkSize := util.AdjustSizes(len(files), PoolSize, MinChunkSize) |
||
| 45 | |||
| 46 | var chunks = make(chan vars.Chunk) |
||
| 47 | var scannedFiles = make(chan vars.File) |
||
| 48 | var wg sync.WaitGroup |
||
| 49 | scancmd.CreatePool( |
||
| 50 | &wg, |
||
| 51 | poolSize, |
||
| 52 | chunks, |
||
| 53 | func(files vars.Chunk) ([]byte, error) { |
||
| 54 | return etool.Read(files, config.App.Fields()) |
||
| 55 | }, |
||
| 56 | func(data []byte) { |
||
| 57 | for _, parsed := range etool.Parse(data) { |
||
| 58 | scannedFiles <- parsed |
||
| 59 | } |
||
| 60 | }, |
||
| 61 | ) |
||
| 62 | |||
| 63 | for _, chunk := range files.Split(chunkSize) { |
||
| 64 | wg.Add(1) |
||
| 65 | chunks <- chunk |
||
| 66 | } |
||
| 67 | |||
| 68 | go func() { |
||
| 69 | wg.Wait() |
||
| 70 | close(chunks) |
||
| 71 | close(scannedFiles) |
||
| 72 | }() |
||
| 73 | |||
| 74 | outputFilename := randomizeOutputFilename(scanFlags.Filename()) |
||
| 75 | |||
| 76 | headers := packHeaders(config.App.Fields()) |
||
| 77 | wr, err := writers.Get(scanFlags.Format()) |
||
| 78 | if err != nil { |
||
| 79 | logWriterFatal(err) |
||
| 80 | } |
||
| 81 | |||
| 82 | err = wr.Open(outputFilename, headers) |
||
| 83 | if err != nil { |
||
| 84 | logWriterFatal(err) |
||
| 85 | } |
||
| 86 | defer wr.Close() |
||
| 87 | |||
| 88 | for file := range scannedFiles { |
||
| 89 | file.WithRelPath(scanFlags.Directory()) |
||
| 90 | err := wr.Write(&file) |
||
| 91 | if err != nil { |
||
| 92 | log.Failure("CSV write", fmt.Sprintf("failed writing data for \"%s\" file", file.RelPath())) |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | log.Log("Scanned", fmt.Sprintf("Scan results are in the \"%s\" file", outputFilename)) |
||
| 97 | } |
||
| 122 |