| Conditions | 24 |
| Total Lines | 71 |
| Code Lines | 59 |
| 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.CronCommand 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 |
||
| 12 | func CronCommand(ctx bot.Context) { |
||
| 13 | if ctx.IsServerAdmin() { |
||
| 14 | // !cron add 0 0 7 * * * !w Chelyabinsk |
||
| 15 | switch ctx.Arg(0) { |
||
| 16 | case "add": |
||
| 17 | ctx.MetricsCommand("cron", "add") |
||
| 18 | if len(ctx.Args) > 7 { |
||
| 19 | if ctx.Args[0] != "*" && ctx.Args[1] != "*" && ctx.Args[2] != "*" { |
||
| 20 | if len(ctx.Args) > 7 { |
||
| 21 | if !ctx.Data.CronIsFull(&ctx) { |
||
| 22 | cmd := strings.Join(ctx.Args[1:], " ") |
||
| 23 | cronTime := strings.Join(ctx.Args[1:7], " ") |
||
| 24 | trigger := ctx.Args[7] |
||
| 25 | ctx.Args = ctx.Args[8:] |
||
| 26 | id, _ := ctx.Cron.AddFunc(cronTime, func() { |
||
| 27 | switch trigger { |
||
| 28 | case "!w": |
||
| 29 | WeatherCommand(ctx) |
||
| 30 | case "!c": |
||
| 31 | CurrencyCommand(ctx) |
||
| 32 | case "!p": |
||
| 33 | PollCommand(ctx) |
||
| 34 | case "!v": |
||
| 35 | VoiceCommand(ctx) |
||
| 36 | case "!y": |
||
| 37 | YoutubeCommand(ctx) |
||
| 38 | case "!play": |
||
| 39 | YoutubeShortCommand(ctx) |
||
| 40 | case "!b": |
||
| 41 | BotCommand(ctx) |
||
| 42 | case "!n": |
||
| 43 | NewsCommand(ctx) |
||
| 44 | } |
||
| 45 | }) |
||
| 46 | _ = ctx.Data.AddCronJob(&ctx, id, cmd) |
||
| 47 | ctx.ReplyEmbedPM("Cron", fmt.Sprintf("Job added: [%v] [%v]", cmd, id)) |
||
| 48 | } else { |
||
| 49 | ctx.ReplyEmbedPM("Cron", "Schedule is full") |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | case "remove": |
||
| 55 | ctx.MetricsCommand("cron", "remove") |
||
| 56 | val, err := strconv.Atoi(ctx.Args[1]) |
||
| 57 | if err != nil { |
||
| 58 | ctx.ReplyEmbedPM("Cron", err.Error()) |
||
| 59 | return |
||
| 60 | } |
||
| 61 | cErr := ctx.Data.CronRemove(&ctx, cron.EntryID(val)) |
||
| 62 | if cErr != nil { |
||
| 63 | ctx.ReplyEmbedPM("Cron", "Error adding job") |
||
| 64 | fmt.Println("Error adding job: ", cErr.Error()) |
||
| 65 | return |
||
| 66 | } |
||
| 67 | ctx.ReplyEmbedPM("Cron", "Job removed") |
||
| 68 | case "list": |
||
| 69 | ctx.MetricsCommand("cron", "list") |
||
| 70 | s, err := ctx.Data.CronList(&ctx) |
||
| 71 | if err != nil { |
||
| 72 | ctx.ReplyEmbedPM("Cron", err.Error()) |
||
| 73 | return |
||
| 74 | } |
||
| 75 | var reply = []string{"Jobs:"} |
||
| 76 | for key, val := range s.CronJobs { |
||
| 77 | reply = append(reply, fmt.Sprintf("[%v] - [%v]", key, val)) |
||
| 78 | } |
||
| 79 | ctx.ReplyEmbedPM("Cron", strings.Join(reply, "\n")) |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | ctx.MetricsCommand("cron", "error") |
||
| 83 | } |
||
| 85 |