Conditions | 16 |
Total Lines | 90 |
Code Lines | 58 |
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 |
||
101 | func initApp() { |
||
102 | fmt.Printf("planfix-toggl %s\n", version) |
||
103 | cfg := config.GetConfig() |
||
104 | |||
105 | parseFlags(&cfg) |
||
106 | |||
107 | logger := getLogger(cfg) |
||
108 | |||
109 | errors, isValid := cfg.Validate() |
||
110 | if !isValid { |
||
111 | for _, e := range errors { |
||
112 | log.Println(e) |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if cfg.NoConsole { |
||
117 | util.HideConsole() |
||
118 | } |
||
119 | |||
120 | togglClient := client.TogglClient{ |
||
121 | Config: &cfg, |
||
122 | Logger: logger, |
||
123 | SentLog: make(map[string]int), |
||
124 | Opts: map[string]string{"LastSent": ""}, |
||
125 | } |
||
126 | togglClient.ReloadConfig() |
||
127 | |||
128 | // get planfix and toggl user IDs, for early API check |
||
129 | err := connectServices(&cfg, logger, &togglClient) |
||
130 | if err != nil { |
||
131 | isValid = false |
||
132 | logger.Printf("[ERROR] %s", err.Error()) |
||
133 | util.Notify(err.Error()) |
||
134 | } |
||
135 | |||
136 | trayMenu["web"].Enable() |
||
137 | trayMenu["log"].Enable() |
||
138 | |||
139 | if isValid { |
||
140 | trayMenu["send"].Enable() |
||
141 | togglClient.Run() |
||
142 | } else { |
||
143 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d", cfg.PortSSL)) |
||
144 | } |
||
145 | |||
146 | // update last sent on menuitem |
||
147 | go func() { |
||
148 | for { |
||
149 | if togglClient.Opts["LastSent"] != "" { |
||
150 | t := togglClient.Opts["LastSent"] |
||
151 | trayMenu["send"].SetTitle(fmt.Sprintf("Sync (last at %s)", t)) |
||
152 | } |
||
153 | time.Sleep(time.Duration(60 * time.Second)) |
||
154 | } |
||
155 | }() |
||
156 | |||
157 | go func() { |
||
158 | // tray menu actions |
||
159 | for { |
||
160 | select { |
||
161 | case <-trayMenu["send"].ClickedCh: |
||
162 | err := togglClient.SendToPlanfix() |
||
163 | t := togglClient.Opts["LastSent"] |
||
164 | trayMenu["send"].SetTitle(fmt.Sprintf("Sync (last at %s)", t)) |
||
165 | if err != nil { |
||
166 | logger.Println(err) |
||
167 | } |
||
168 | |||
169 | case <-trayMenu["web"].ClickedCh: |
||
170 | cfg := config.GetConfig() |
||
171 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d", cfg.PortSSL)) |
||
172 | |||
173 | case <-trayMenu["log"].ClickedCh: |
||
174 | cfg := config.GetConfig() |
||
175 | systray.ShowAppWindow(fmt.Sprintf("https://localhost:%d/log", cfg.PortSSL)) |
||
176 | |||
177 | case <-trayMenu["quit"].ClickedCh: |
||
178 | onExit() |
||
179 | } |
||
180 | } |
||
181 | }() |
||
182 | |||
183 | // start API server |
||
184 | server := rest.Server{ |
||
185 | Version: version, |
||
186 | TogglClient: &togglClient, |
||
187 | Config: &cfg, |
||
188 | Logger: logger, |
||
189 | } |
||
190 | server.Run(cfg.PortSSL) |
||
191 | } |
||
221 |