| Conditions | 12 |
| Total Lines | 67 |
| 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 main.initApp 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 main |
||
| 105 | func initApp() { |
||
| 106 | fmt.Printf("planfix-toggl %s\n", version) |
||
| 107 | cfg := config.GetConfig() |
||
| 108 | |||
| 109 | parseFlags(&cfg) |
||
| 110 | |||
| 111 | logger := getLogger(cfg) |
||
| 112 | |||
| 113 | errors, isValid := cfg.Validate() |
||
| 114 | if !isValid { |
||
| 115 | for _, e := range errors { |
||
| 116 | log.Println(e) |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | if cfg.NoConsole { |
||
| 121 | util.HideConsole() |
||
| 122 | } |
||
| 123 | |||
| 124 | togglClient := client.TogglClient{ |
||
| 125 | Config: &cfg, |
||
| 126 | Logger: logger, |
||
| 127 | } |
||
| 128 | togglClient.ReloadConfig() |
||
| 129 | |||
| 130 | // get planfix and toggl user IDs, for early API check |
||
| 131 | err := connectServices(&cfg, logger, &togglClient) |
||
| 132 | if err != nil { |
||
| 133 | isValid = false |
||
| 134 | logger.Printf("[ERROR] %s", err.Error()) |
||
| 135 | Notify(err.Error()) |
||
| 136 | } |
||
| 137 | |||
| 138 | if isValid { |
||
| 139 | togglClient.Run() |
||
| 140 | } else { |
||
| 141 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d", cfg.PortSSL)) |
||
| 142 | } |
||
| 143 | |||
| 144 | go func() { |
||
| 145 | // tray menu actions |
||
| 146 | for { |
||
| 147 | select { |
||
| 148 | case <-trayMenu["web"].ClickedCh: |
||
| 149 | cfg := config.GetConfig() |
||
| 150 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d", cfg.PortSSL)) |
||
| 151 | |||
| 152 | case <-trayMenu["send"].ClickedCh: |
||
| 153 | err := togglClient.SendToPlanfix() |
||
| 154 | if err != nil { |
||
| 155 | logger.Println(err) |
||
| 156 | } |
||
| 157 | |||
| 158 | case <-trayMenu["quit"].ClickedCh: |
||
| 159 | onExit() |
||
| 160 | } |
||
| 161 | } |
||
| 162 | }() |
||
| 163 | |||
| 164 | // start API server |
||
| 165 | server := rest.Server{ |
||
| 166 | Version: version, |
||
| 167 | TogglClient: &togglClient, |
||
| 168 | Config: &cfg, |
||
| 169 | Logger: logger, |
||
| 170 | } |
||
| 171 | server.Run(cfg.PortSSL) |
||
| 172 | } |
||
| 196 |