| Conditions | 10 |
| Total Lines | 42 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like operations.ScanFiles 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 operations |
||
| 11 | func ScanFiles(fileChunks []vars.Chunk, poolSize int) []vars.File { |
||
| 12 | var done = make(chan struct{}) |
||
| 13 | |||
| 14 | var wg sync.WaitGroup |
||
| 15 | var chunks = make(chan vars.Chunk) |
||
| 16 | var scannedFiles = make(chan vars.File) |
||
| 17 | scancmd.CreatePool( |
||
| 18 | &wg, |
||
| 19 | poolSize, |
||
| 20 | chunks, |
||
| 21 | func(files vars.Chunk) ([]byte, error) { |
||
| 22 | return etool.Read(files, config.App.Fields()) |
||
| 23 | }, |
||
| 24 | func(data []byte) { |
||
| 25 | for _, parsed := range etool.Parse(data) { |
||
| 26 | scannedFiles <- parsed |
||
| 27 | } |
||
| 28 | }, |
||
| 29 | ) |
||
| 30 | |||
| 31 | for _, chunk := range fileChunks { |
||
| 32 | wg.Add(1) |
||
| 33 | chunks <- chunk |
||
| 34 | } |
||
| 35 | |||
| 36 | go func() { |
||
| 37 | wg.Wait() |
||
| 38 | close(scannedFiles) |
||
| 39 | close(chunks) |
||
| 40 | done <- struct{}{} |
||
| 41 | }() |
||
| 42 | |||
| 43 | var files []vars.File |
||
| 44 | |||
| 45 | for file := range scannedFiles { |
||
| 46 | files = append(files, file) |
||
| 47 | } |
||
| 48 | |||
| 49 | <-done |
||
| 50 | close(done) |
||
| 51 | |||
| 52 | return files |
||
| 53 | } |
||
| 54 |