Conditions | 14 |
Total Lines | 76 |
Code Lines | 40 |
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 action.Get 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 action |
||
21 | func Get(names []string, installer *repo.Installer, insecure, skipRecursive, stripVendor, nonInteract, testDeps bool) { |
||
22 | cache.SystemLock() |
||
23 | |||
24 | base := gpath.Basepath() |
||
25 | EnsureGopath() |
||
26 | EnsureVendorDir() |
||
27 | conf := EnsureConfig() |
||
28 | glidefile, err := gpath.Glide() |
||
29 | if err != nil { |
||
30 | msg.Die("Could not find Glide file: %s", err) |
||
31 | } |
||
32 | |||
33 | // Add the packages to the config. |
||
34 | if count, err2 := addPkgsToConfig(conf, names, insecure, nonInteract, testDeps); err2 != nil { |
||
35 | msg.Die("Failed to get new packages: %s", err2) |
||
36 | } else if count == 0 { |
||
37 | msg.Warn("Nothing to do") |
||
38 | return |
||
39 | } |
||
40 | |||
41 | // Fetch the new packages. Can't resolve versions via installer.Update if |
||
42 | // get is called while the vendor/ directory is empty so we checkout |
||
43 | // everything. |
||
44 | err = installer.Checkout(conf) |
||
45 | if err != nil { |
||
46 | msg.Die("Failed to checkout packages: %s", err) |
||
47 | } |
||
48 | |||
49 | // Prior to resolving dependencies we need to start working with a clone |
||
50 | // of the conf because we'll be making real changes to it. |
||
51 | confcopy := conf.Clone() |
||
52 | |||
53 | if !skipRecursive { |
||
54 | // Get all repos and update them. |
||
55 | // TODO: Can we streamline this in any way? The reason that we update all |
||
56 | // of the dependencies is that we need to re-negotiate versions. For example, |
||
57 | // if an existing dependency has the constraint >1.0 and this new package |
||
58 | // adds the constraint <2.0, then this may re-resolve the existing dependency |
||
59 | // to be between 1.0 and 2.0. But changing that dependency may then result |
||
60 | // in that dependency's dependencies changing... so we sorta do the whole |
||
61 | // thing to be safe. |
||
62 | err = installer.Update(confcopy) |
||
63 | if err != nil { |
||
64 | msg.Die("Could not update packages: %s", err) |
||
65 | } |
||
66 | } |
||
67 | |||
68 | // Set Reference |
||
69 | if err := repo.SetReference(confcopy, installer.ResolveTest); err != nil { |
||
70 | msg.Err("Failed to set references: %s", err) |
||
71 | } |
||
72 | |||
73 | err = installer.Export(confcopy) |
||
74 | if err != nil { |
||
75 | msg.Die("Unable to export dependencies to vendor directory: %s", err) |
||
76 | } |
||
77 | |||
78 | // Write YAML |
||
79 | if err := conf.WriteFile(glidefile); err != nil { |
||
80 | msg.Die("Failed to write glide YAML file: %s", err) |
||
81 | } |
||
82 | if !skipRecursive { |
||
83 | // Write lock |
||
84 | if stripVendor { |
||
85 | confcopy = godep.RemoveGodepSubpackages(confcopy) |
||
86 | } |
||
87 | writeLock(conf, confcopy, base) |
||
88 | } else { |
||
89 | msg.Warn("Skipping lockfile generation because full dependency tree is not being calculated") |
||
90 | } |
||
91 | |||
92 | if stripVendor { |
||
93 | msg.Info("Removing nested vendor and Godeps/_workspace directories...") |
||
94 | err := gpath.StripVendor() |
||
95 | if err != nil { |
||
96 | msg.Err("Unable to strip vendor directories: %s", err) |
||
97 | } |
||
252 |