Conditions | 23 |
Total Lines | 85 |
Code Lines | 68 |
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.main 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 |
||
45 | func main() { |
||
46 | flag.Parse() |
||
47 | switch *loglevel { |
||
48 | case "panic": |
||
49 | log.SetLevel(log.PanicLevel) |
||
50 | case "fatal": |
||
51 | log.SetLevel(log.FatalLevel) |
||
52 | case "error": |
||
53 | log.SetLevel(log.ErrorLevel) |
||
54 | case "warn": |
||
55 | log.SetLevel(log.WarnLevel) |
||
56 | case "info": |
||
57 | log.SetLevel(log.InfoLevel) |
||
58 | case "debug": |
||
59 | log.SetLevel(log.DebugLevel) |
||
60 | case "trace": |
||
61 | log.SetLevel(log.DebugLevel) |
||
62 | default: |
||
63 | log.Fatalf("Invalid log level value %s\n", *loglevel) |
||
64 | os.Exit(1) |
||
65 | } |
||
66 | |||
67 | capacity, err := bytesize.Parse(*capacityStr) |
||
68 | |||
69 | if err != nil { |
||
70 | log.Fatal("Invalid value for capacity", err.Error()) |
||
71 | } |
||
72 | |||
73 | if strings.ToLower(*kind) == "lru" { |
||
74 | gerdu = lrucache.NewCache(capacity) |
||
75 | } else if strings.ToLower(*kind) == "lfu" { |
||
76 | gerdu = lfucache.NewCache(capacity) |
||
77 | } else if strings.ToLower(*kind) == "weak" { |
||
78 | gerdu = weakcache.NewWeakCache() |
||
79 | } else { |
||
80 | log.Fatalf("Invalid value for type") |
||
81 | os.Exit(1) |
||
82 | } |
||
83 | |||
84 | *protocols = strings.ToLower(*protocols) |
||
85 | var validProtocol bool |
||
86 | if strings.Contains(*protocols, "http") { |
||
87 | validProtocol = true |
||
88 | wg.Add(1) |
||
89 | go func() { |
||
90 | defer wg.Done() |
||
91 | httpHost := *host + ":" + strconv.Itoa(*httpPort) |
||
92 | if secure { |
||
93 | httpserver.HttpServeTLS(httpHost, *tlsCert, *tlsKey, gerdu) |
||
94 | } else { |
||
95 | httpserver.HttpServe(httpHost, gerdu) |
||
96 | } |
||
97 | }() |
||
98 | } |
||
99 | if strings.Contains(*protocols, "grpc") { |
||
100 | validProtocol = true |
||
101 | wg.Add(1) |
||
102 | go func() { |
||
103 | defer wg.Done() |
||
104 | grpcHost := *host + ":" + strconv.Itoa(*grpcPort) |
||
105 | if secure { |
||
106 | grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu) |
||
107 | } else { |
||
108 | grpcserver.GrpcServe(grpcHost, gerdu) |
||
109 | } |
||
110 | }() |
||
111 | } |
||
112 | if strings.Contains(*protocols, "mcd") { |
||
113 | validProtocol = true |
||
114 | wg.Add(1) |
||
115 | go func() { |
||
116 | defer wg.Done() |
||
117 | mcdHost := *host + ":" + strconv.Itoa(*mcdPort) |
||
118 | if secure { |
||
119 | log.Fatalln("Memcached protocol does not support TLS") |
||
120 | os.Exit(1) |
||
121 | } |
||
122 | memcached.MemcachedServe(mcdHost, gerdu) |
||
123 | }() |
||
124 | } |
||
125 | if !validProtocol { |
||
126 | log.Fatalf("Invalid value for protocol") |
||
127 | os.Exit(1) |
||
128 | } |
||
129 | wg.Wait() |
||
130 | } |
||
131 |