Conditions | 13 |
Total Lines | 52 |
Code Lines | 37 |
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 |
||
59 | func serve() { |
||
60 | *protocols = strings.ToLower(*protocols) |
||
61 | |||
62 | go func() { |
||
63 | httpHost := *host + ":" + strconv.Itoa(*httpPort) |
||
64 | if secure { |
||
65 | httpserver.HTTPServeTLS(httpHost, *tlsCert, *tlsKey, gerdu) |
||
66 | } else { |
||
67 | httpserver.HTTPServe(httpHost, gerdu) |
||
68 | } |
||
69 | }() |
||
70 | |||
71 | if strings.Contains(*protocols, "grpc") { |
||
72 | go func() { |
||
73 | grpcHost := *host + ":" + strconv.Itoa(*grpcPort) |
||
74 | if secure { |
||
75 | grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu) |
||
76 | } else { |
||
77 | grpcserver.GrpcServe(grpcHost, gerdu) |
||
78 | } |
||
79 | }() |
||
80 | } |
||
81 | if strings.Contains(*protocols, "mcd") { |
||
82 | go func() { |
||
83 | mcdHost := *host + ":" + strconv.Itoa(*mcdPort) |
||
84 | if secure { |
||
85 | log.Fatalln("Memcached protocol does not support TLS") |
||
86 | os.Exit(1) |
||
87 | } |
||
88 | memcached.Serve(mcdHost, gerdu) |
||
89 | }() |
||
90 | } |
||
91 | |||
92 | if strings.Contains(*protocols, "redis") { |
||
93 | go func() { |
||
94 | redisHost := *host + ":" + strconv.Itoa(*redisPort) |
||
95 | if secure { |
||
96 | redis.ServeTLS(redisHost, *tlsCert, *tlsKey, gerdu) |
||
97 | } else { |
||
98 | redis.Serve(redisHost, gerdu) |
||
99 | } |
||
100 | }() |
||
101 | } |
||
102 | |||
103 | terminate := make(chan os.Signal, 1) |
||
104 | signal.Notify(terminate, os.Interrupt) |
||
105 | <-terminate |
||
106 | err := gerdu.(*raftproxy.RaftProxy).Leave(*nodeID) |
||
107 | if err != nil { |
||
108 | log.Errorf("Cannot leave the cluster gracefully %v", err) |
||
109 | } else { |
||
110 | log.Println("Gerdu exiting") |
||
111 | } |
||
161 |