| Conditions | 20 |
| Total Lines | 56 |
| Code Lines | 44 |
| 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 telemetry.OtelHandler.slogAttrToOtelAttr 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 telemetry |
||
| 145 | func (h OtelHandler) slogAttrToOtelAttr(attr slog.Attr, groupKeys ...string) attribute.KeyValue { |
||
| 146 | attr.Value = attr.Value.Resolve() |
||
| 147 | if attr.Equal(slog.Attr{}) { |
||
| 148 | return attribute.KeyValue{} |
||
| 149 | } |
||
| 150 | |||
| 151 | key := func(k string, prefixes ...string) string { |
||
| 152 | for _, prefix := range prefixes { |
||
| 153 | k = fmt.Sprintf("%s.%s", prefix, k) |
||
| 154 | } |
||
| 155 | |||
| 156 | return k |
||
| 157 | }(attr.Key, groupKeys...) |
||
| 158 | |||
| 159 | value := attr.Value.Resolve() |
||
| 160 | |||
| 161 | switch attr.Value.Kind() { |
||
| 162 | case slog.KindBool: |
||
| 163 | return attribute.Bool(key, value.Bool()) |
||
| 164 | case slog.KindFloat64: |
||
| 165 | return attribute.Float64(key, value.Float64()) |
||
| 166 | case slog.KindInt64: |
||
| 167 | return attribute.Int64(key, value.Int64()) |
||
| 168 | case slog.KindString: |
||
| 169 | return attribute.String(key, value.String()) |
||
| 170 | case slog.KindTime: |
||
| 171 | return attribute.String(key, value.Time().Format(time.RFC3339Nano)) |
||
| 172 | case slog.KindGroup: |
||
| 173 | groupAttrs := value.Group() |
||
| 174 | if len(groupAttrs) == 0 { |
||
| 175 | return attribute.KeyValue{} |
||
| 176 | } |
||
| 177 | |||
| 178 | for _, groupAttr := range groupAttrs { |
||
| 179 | return h.slogAttrToOtelAttr(groupAttr, append(groupKeys, key)...) |
||
| 180 | } |
||
| 181 | case slog.KindAny: |
||
| 182 | switch v := attr.Value.Any().(type) { |
||
| 183 | case []string: |
||
| 184 | return attribute.StringSlice(key, v) |
||
| 185 | case []int: |
||
| 186 | return attribute.IntSlice(key, v) |
||
| 187 | case []int64: |
||
| 188 | return attribute.Int64Slice(key, v) |
||
| 189 | case []float64: |
||
| 190 | return attribute.Float64Slice(key, v) |
||
| 191 | case []bool: |
||
| 192 | return attribute.BoolSlice(key, v) |
||
| 193 | default: |
||
| 194 | return attribute.KeyValue{} |
||
| 195 | } |
||
| 196 | default: |
||
| 197 | return attribute.KeyValue{} |
||
| 198 | } |
||
| 199 | |||
| 200 | return attribute.KeyValue{} |
||
| 201 | } |
||
| 202 |