Conditions | 17 |
Total Lines | 63 |
Code Lines | 50 |
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 gom.parseGomfile 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 gom |
||
66 | func parseGomfile(filename string) ([]Gom, error) { |
||
67 | f, err := os.Open(filename + ".lock") |
||
68 | if err != nil { |
||
69 | f, err = os.Open(filename) |
||
70 | if err != nil { |
||
71 | return nil, err |
||
72 | } |
||
73 | } |
||
74 | br := bufio.NewReader(f) |
||
75 | |||
76 | goms := make([]Gom, 0) |
||
77 | |||
78 | n := 0 |
||
79 | skip := 0 |
||
80 | valid := true |
||
81 | var envs []string |
||
82 | for { |
||
83 | n++ |
||
84 | lb, _, err := br.ReadLine() |
||
85 | if err != nil { |
||
86 | if err == io.EOF { |
||
87 | return goms, nil |
||
88 | } |
||
89 | return nil, err |
||
90 | } |
||
91 | line := strings.TrimSpace(string(lb)) |
||
92 | if line == "" || strings.HasPrefix(line, "#") { |
||
93 | continue |
||
94 | } |
||
95 | |||
96 | name := "" |
||
97 | options := make(map[string]interface{}) |
||
98 | var items []string |
||
99 | if reGroup.MatchString(line) { |
||
100 | envs = strings.Split(reGroup.FindStringSubmatch(line)[1], ",") |
||
101 | for i := range envs { |
||
102 | envs[i] = strings.TrimSpace(envs[i])[1:] |
||
103 | } |
||
104 | valid = true |
||
105 | continue |
||
106 | } else if reEnd.MatchString(line) { |
||
107 | if !valid { |
||
108 | skip-- |
||
109 | if skip < 0 { |
||
110 | return nil, fmt.Errorf("Syntax Error at line %d", n) |
||
111 | } |
||
112 | } |
||
113 | valid = false |
||
114 | envs = nil |
||
115 | continue |
||
116 | } else if skip > 0 { |
||
117 | continue |
||
118 | } else if reGom.MatchString(line) { |
||
119 | items = reGom.FindStringSubmatch(line)[1:] |
||
120 | name = unquote(items[0]) |
||
121 | parseOptions(items[1], options) |
||
122 | } else { |
||
123 | return nil, fmt.Errorf("Syntax Error at line %d", n) |
||
124 | } |
||
125 | if envs != nil { |
||
126 | options["group"] = envs |
||
127 | } |
||
128 | goms = append(goms, Gom{name, options}) |
||
129 | } |
||
131 |