| Conditions | 16 |
| Total Lines | 63 |
| Code Lines | 42 |
| 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 strip.rewriteGodepfilesHandler 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 strip removes Godeps/_workspace and undoes the Godep rewrites. This |
||
| 83 | func rewriteGodepfilesHandler(path string, info os.FileInfo, err error) error { |
||
| 84 | name := info.Name() |
||
| 85 | |||
| 86 | if info.IsDir() { |
||
| 87 | if name == "testdata" || name == "vendor" { |
||
| 88 | return filepath.SkipDir |
||
| 89 | } |
||
| 90 | return nil |
||
| 91 | } |
||
| 92 | |||
| 93 | if e := filepath.Ext(path); e != ".go" { |
||
| 94 | return nil |
||
| 95 | } |
||
| 96 | |||
| 97 | fset := token.NewFileSet() |
||
| 98 | f, err := parser.ParseFile(fset, path, nil, parser.ParseComments) |
||
| 99 | if err != nil { |
||
| 100 | return err |
||
| 101 | } |
||
| 102 | |||
| 103 | var changed bool |
||
| 104 | for _, s := range f.Imports { |
||
| 105 | n, err := strconv.Unquote(s.Path.Value) |
||
| 106 | if err != nil { |
||
| 107 | return err |
||
| 108 | } |
||
| 109 | q := rewriteGodepImport(n) |
||
| 110 | if q != name { |
||
| 111 | s.Path.Value = strconv.Quote(q) |
||
| 112 | changed = true |
||
| 113 | } |
||
| 114 | } |
||
| 115 | if !changed { |
||
| 116 | return nil |
||
| 117 | } |
||
| 118 | |||
| 119 | printerConfig := &printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8} |
||
| 120 | var buffer bytes.Buffer |
||
| 121 | if err = printerConfig.Fprint(&buffer, fset, f); err != nil { |
||
| 122 | return err |
||
| 123 | } |
||
| 124 | fset = token.NewFileSet() |
||
| 125 | f, err = parser.ParseFile(fset, name, &buffer, parser.ParseComments) |
||
| 126 | ast.SortImports(fset, f) |
||
| 127 | tpath := path + ".temp" |
||
| 128 | t, err := os.Create(tpath) |
||
| 129 | if err != nil { |
||
| 130 | return err |
||
| 131 | } |
||
| 132 | if err = printerConfig.Fprint(t, fset, f); err != nil { |
||
| 133 | return err |
||
| 134 | } |
||
| 135 | if err = t.Close(); err != nil { |
||
| 136 | return err |
||
| 137 | } |
||
| 138 | |||
| 139 | msg.Debug("Rewriting Godep imports for %s", path) |
||
| 140 | |||
| 141 | // This is required before the rename on windows. |
||
| 142 | if err = os.Remove(path); err != nil { |
||
| 143 | return err |
||
| 144 | } |
||
| 145 | return os.Rename(tpath, path) |
||
| 146 | } |
||
| 157 |