| Conditions | 20 |
| Total Lines | 70 |
| Code Lines | 54 |
| 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 cmd.botGuild 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 cmd |
||
| 172 | func botGuild(ctx *bot.Context) { |
||
| 173 | ctx.MetricsCommand("bot", "guild") |
||
| 174 | if len(ctx.Args) < 2 && !ctx.IsAdmin() { |
||
| 175 | return |
||
| 176 | } |
||
| 177 | switch ctx.Args[1] { |
||
| 178 | case "leave": |
||
| 179 | if len(ctx.Args) < 3 { |
||
| 180 | return |
||
| 181 | } |
||
| 182 | err := ctx.Discord.GuildLeave(ctx.Args[2]) |
||
| 183 | if err != nil { |
||
| 184 | ctx.Log("Guild", ctx.Guild.ID, fmt.Sprintf("error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 185 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 186 | return |
||
| 187 | } |
||
| 188 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Leave from guild: %v", ctx.Args[2])) |
||
| 189 | case "list": |
||
| 190 | var selected string |
||
| 191 | var paged = false |
||
| 192 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 193 | if len(ctx.Args) > 3 { |
||
| 194 | selected = ctx.Args[3] |
||
| 195 | paged = true |
||
| 196 | } else { |
||
| 197 | selected = "1" |
||
| 198 | } |
||
| 199 | } else { |
||
| 200 | if len(ctx.Args) > 2 { |
||
| 201 | selected = ctx.Args[2] |
||
| 202 | paged = true |
||
| 203 | } else { |
||
| 204 | selected = "1" |
||
| 205 | } |
||
| 206 | } |
||
| 207 | // calculates count of pages |
||
| 208 | guilds := ctx.Discord.State.Guilds |
||
| 209 | pages := 1 + int(len(guilds)/20) |
||
| 210 | // paginate |
||
| 211 | var indexTo = 20 |
||
| 212 | if paged { |
||
| 213 | page, err := strconv.Atoi(selected) |
||
| 214 | if err == nil { |
||
| 215 | indexTo = page * 20 |
||
| 216 | indexFrom := indexTo - 20 |
||
| 217 | |||
| 218 | if indexFrom < 0 { |
||
| 219 | indexFrom = 0 |
||
| 220 | } |
||
| 221 | if indexTo > len(guilds) { |
||
| 222 | indexTo = len(guilds) - 1 |
||
| 223 | } |
||
| 224 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 225 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[indexFrom:indexTo], page, pages)+fmt.Sprintf("\nFrom: %v\nTo: %v", indexFrom, indexTo)) |
||
| 226 | } else { |
||
| 227 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[indexFrom:indexTo], page, pages)+fmt.Sprintf("\nFrom: %v\nTo: %v", indexFrom, indexTo)) |
||
| 228 | } |
||
| 229 | |||
| 230 | } else { |
||
| 231 | ctx.ReplyEmbed("Guilds", fmt.Sprintf("Selected: %v\nError: %v", selected, err.Error())) |
||
| 232 | } |
||
| 233 | |||
| 234 | } else { |
||
| 235 | if indexTo > len(guilds) { |
||
| 236 | indexTo = len(guilds) - 1 |
||
| 237 | } |
||
| 238 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 239 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[:indexTo], 1, 1)+fmt.Sprintf("\nTo: %v", indexTo)) |
||
| 240 | } else { |
||
| 241 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[:indexTo], 1, 1)+fmt.Sprintf("\nTo: %v", indexTo)) |
||
| 242 | } |
||
| 274 | } |