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