| Conditions | 26 | 
| Total Lines | 101 | 
| Code Lines | 66 | 
| 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.addPkgsToConfig 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 | ||
| 124 | func addPkgsToConfig(conf *cfg.Config, names []string, insecure, nonInteract, testDeps bool) (int, error) { | ||
| 125 | |||
| 126 | 	if len(names) == 1 { | ||
| 127 | 		msg.Info("Preparing to install %d package.", len(names)) | ||
| 128 | 	} else { | ||
| 129 | 		msg.Info("Preparing to install %d packages.", len(names)) | ||
| 130 | } | ||
| 131 | numAdded := 0 | ||
| 132 | 	for _, name := range names { | ||
| 133 | var version string | ||
| 134 | parts := strings.Split(name, "#") | ||
| 135 | 		if len(parts) > 1 { | ||
| 136 | name = parts[0] | ||
| 137 | version = parts[1] | ||
| 138 | } | ||
| 139 | |||
| 140 | 		msg.Info("Attempting to get package %s", name) | ||
| 141 | |||
| 142 | root, subpkg := util.NormalizeName(name) | ||
| 143 | 		if len(root) == 0 { | ||
| 144 | 			return 0, fmt.Errorf("Package name is required for %q.", name) | ||
|  | |||
| 145 | } | ||
| 146 | |||
| 147 | 		if conf.HasDependency(root) { | ||
| 148 | |||
| 149 | var moved bool | ||
| 150 | var dep *cfg.Dependency | ||
| 151 | // Move from DevImports to Imports | ||
| 152 | 			if !testDeps && !conf.Imports.Has(root) && conf.DevImports.Has(root) { | ||
| 153 | dep = conf.DevImports.Get(root) | ||
| 154 | conf.Imports = append(conf.Imports, dep) | ||
| 155 | conf.DevImports = conf.DevImports.Remove(root) | ||
| 156 | moved = true | ||
| 157 | numAdded++ | ||
| 158 | 				msg.Info("--> Moving %s from testImport to import", root) | ||
| 159 | 			} else if testDeps && conf.Imports.Has(root) { | ||
| 160 | 				msg.Warn("--> Test dependency %s already listed as import", root) | ||
| 161 | } | ||
| 162 | |||
| 163 | // Check if the subpackage is present. | ||
| 164 | 			if subpkg != "" { | ||
| 165 | 				if dep == nil { | ||
| 166 | dep = conf.Imports.Get(root) | ||
| 167 | 					if dep == nil && testDeps { | ||
| 168 | dep = conf.DevImports.Get(root) | ||
| 169 | } | ||
| 170 | } | ||
| 171 | 				if dep.HasSubpackage(subpkg) { | ||
| 172 | 					if !moved { | ||
| 173 | 						msg.Warn("--> Package %q is already in glide.yaml. Skipping", name) | ||
| 174 | } | ||
| 175 | 				} else { | ||
| 176 | dep.Subpackages = append(dep.Subpackages, subpkg) | ||
| 177 | 					msg.Info("--> Adding sub-package %s to existing import %s", subpkg, root) | ||
| 178 | numAdded++ | ||
| 179 | } | ||
| 180 | 			} else if !moved { | ||
| 181 | 				msg.Warn("--> Package %q is already in glide.yaml. Skipping", root) | ||
| 182 | } | ||
| 183 | continue | ||
| 184 | } | ||
| 185 | |||
| 186 | 		if conf.HasIgnore(root) { | ||
| 187 | 			msg.Warn("--> Package %q is set to be ignored in glide.yaml. Skipping", root) | ||
| 188 | continue | ||
| 189 | } | ||
| 190 | |||
| 191 | 		dep := &cfg.Dependency{ | ||
| 192 | Name: root, | ||
| 193 | } | ||
| 194 | |||
| 195 | // When retriving from an insecure location set the repo to the | ||
| 196 | // insecure location. | ||
| 197 | 		if insecure { | ||
| 198 | dep.Repository = "http://" + root | ||
| 199 | } | ||
| 200 | |||
| 201 | 		if version != "" { | ||
| 202 | dep.Reference = version | ||
| 203 | 		} else if !nonInteract { | ||
| 204 | getWizard(dep) | ||
| 205 | } | ||
| 206 | |||
| 207 | 		if len(subpkg) > 0 { | ||
| 208 | 			dep.Subpackages = []string{subpkg} | ||
| 209 | } | ||
| 210 | |||
| 211 | 		if dep.Reference != "" { | ||
| 212 | 			msg.Info("--> Adding %s to your configuration with the version %s", dep.Name, dep.Reference) | ||
| 213 | 		} else { | ||
| 214 | 			msg.Info("--> Adding %s to your configuration", dep.Name) | ||
| 215 | } | ||
| 216 | |||
| 217 | 		if testDeps { | ||
| 218 | conf.DevImports = append(conf.DevImports, dep) | ||
| 219 | 		} else { | ||
| 220 | conf.Imports = append(conf.Imports, dep) | ||
| 221 | } | ||
| 222 | numAdded++ | ||
| 223 | } | ||
| 224 | return numAdded, nil | ||
| 225 | } | ||
| 252 |