Conditions | 12 |
Total Lines | 58 |
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.serve 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 |
||
61 | func serve() { |
||
62 | *protocols = strings.ToLower(*protocols) |
||
63 | |||
64 | wg.Add(1) |
||
65 | go func() { |
||
66 | defer wg.Done() |
||
67 | httpHost := *host + ":" + strconv.Itoa(*httpPort) |
||
68 | if secure { |
||
69 | httpserver.HTTPServeTLS(httpHost, *tlsCert, *tlsKey, gerdu) |
||
70 | } else { |
||
71 | httpserver.HTTPServe(httpHost, gerdu) |
||
72 | } |
||
73 | }() |
||
74 | |||
75 | if strings.Contains(*protocols, "grpc") { |
||
76 | wg.Add(1) |
||
77 | go func() { |
||
78 | defer wg.Done() |
||
79 | grpcHost := *host + ":" + strconv.Itoa(*grpcPort) |
||
80 | if secure { |
||
81 | grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu) |
||
82 | } else { |
||
83 | grpcserver.GrpcServe(grpcHost, gerdu) |
||
84 | } |
||
85 | }() |
||
86 | } |
||
87 | if strings.Contains(*protocols, "mcd") { |
||
88 | wg.Add(1) |
||
89 | go func() { |
||
90 | defer wg.Done() |
||
91 | mcdHost := *host + ":" + strconv.Itoa(*mcdPort) |
||
92 | if secure { |
||
93 | log.Fatalln("Memcached protocol does not support TLS") |
||
94 | os.Exit(1) |
||
95 | } |
||
96 | memcached.Serve(mcdHost, gerdu) |
||
97 | }() |
||
98 | } |
||
99 | |||
100 | if strings.Contains(*protocols, "redis") { |
||
101 | wg.Add(1) |
||
102 | go func() { |
||
103 | defer wg.Done() |
||
104 | redisHost := *host + ":" + strconv.Itoa(*redisPort) |
||
105 | if secure { |
||
106 | redis.ServeTLS(redisHost, *tlsCert, *tlsKey, gerdu) |
||
107 | } else { |
||
108 | redis.Serve(redisHost, gerdu) |
||
109 | } |
||
110 | }() |
||
111 | } |
||
112 | |||
113 | wg.Wait() |
||
114 | |||
115 | terminate := make(chan os.Signal, 1) |
||
116 | signal.Notify(terminate, os.Interrupt) |
||
117 | <-terminate |
||
118 | log.Println("Gerdu exiting") |
||
119 | } |
||
168 |