| Conditions | 17 |
| Total Lines | 77 |
| Code Lines | 44 |
| 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 snapshot.Token.createFinalSnapshot 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 snapshot |
||
| 131 | func (t Token) createFinalSnapshot() string { |
||
| 132 | if t.Snapshot == "" { |
||
| 133 | return t.Snapshot |
||
| 134 | } |
||
| 135 | |||
| 136 | parts := strings.SplitN(strings.TrimSpace(t.Snapshot), ":", 3) |
||
| 137 | if len(parts) < 2 { |
||
| 138 | return t.Snapshot |
||
| 139 | } |
||
| 140 | |||
| 141 | xmin, err := strconv.ParseUint(parts[0], 10, 64) |
||
| 142 | if err != nil { |
||
| 143 | return t.Snapshot |
||
| 144 | } |
||
| 145 | xmax, err := strconv.ParseUint(parts[1], 10, 64) |
||
| 146 | if err != nil { |
||
| 147 | return t.Snapshot |
||
| 148 | } |
||
| 149 | |||
| 150 | txid := t.Value.Uint |
||
| 151 | |||
| 152 | // Parse existing XIPs |
||
| 153 | xipList := []uint64{} |
||
| 154 | if len(parts) == 3 && parts[2] != "" { |
||
| 155 | for _, xipStr := range strings.Split(parts[2], ",") { |
||
| 156 | xipStr = strings.TrimSpace(xipStr) |
||
| 157 | if xipStr == "" { |
||
| 158 | continue |
||
| 159 | } |
||
| 160 | xip, err := strconv.ParseUint(xipStr, 10, 64) |
||
| 161 | if err != nil { |
||
| 162 | return t.Snapshot |
||
| 163 | } |
||
| 164 | xipList = append(xipList, xip) |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // Add current txid to make snapshot unique for this transaction |
||
| 169 | // Insert in sorted order to maintain consistency |
||
| 170 | inserted := false |
||
| 171 | for i, xip := range xipList { |
||
| 172 | if xip == txid { |
||
| 173 | // Already in list, don't add again |
||
| 174 | inserted = true |
||
| 175 | break |
||
| 176 | } |
||
| 177 | if xip > txid { |
||
| 178 | // Insert at position i |
||
| 179 | xipList = append(xipList[:i], append([]uint64{txid}, xipList[i:]...)...) |
||
| 180 | inserted = true |
||
| 181 | break |
||
| 182 | } |
||
| 183 | } |
||
| 184 | if !inserted { |
||
| 185 | // Append to end |
||
| 186 | xipList = append(xipList, txid) |
||
| 187 | } |
||
| 188 | |||
| 189 | // Adjust xmax if necessary |
||
| 190 | newXmax := xmax |
||
| 191 | if txid >= newXmax { |
||
| 192 | newXmax = txid + 1 |
||
| 193 | } |
||
| 194 | |||
| 195 | // Adjust xmin if current txid is smaller |
||
| 196 | newXmin := xmin |
||
| 197 | if txid < newXmin { |
||
| 198 | newXmin = txid |
||
| 199 | } |
||
| 200 | |||
| 201 | // Build the result snapshot string |
||
| 202 | var xipStrs []string |
||
| 203 | for _, xip := range xipList { |
||
| 204 | xipStrs = append(xipStrs, fmt.Sprintf("%d", xip)) |
||
| 205 | } |
||
| 206 | |||
| 207 | return fmt.Sprintf("%d:%d:%s", newXmin, newXmax, strings.Join(xipStrs, ",")) |
||
| 208 | } |
||
| 209 |