prometheus/discovery/kubernetes
beorn7 5b53aa1108 style: Replace else if cascades with switch
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.

The exceptions that I have found in our codebase are just these two:

* The `if else` is followed by an additional statement before the next
  condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
  used. In this case, using `switch` would require tagging the `for`
  loop, which probably tips the balance.

Why are `switch` statements more readable?

For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.

I'm sure the aforemention wise coders can list even more reasons.

In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.

Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-19 17:22:31 +02:00
..
client_metrics.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
endpoints.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
endpoints_test.go discovery/kubernetes: fix broken tests 2022-07-18 09:10:12 -04:00
endpointslice.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
endpointslice_adaptor.go Add support for EndpointSlice conditions 2023-01-19 18:56:02 +01:00
endpointslice_adaptor_test.go Add support for EndpointSlice conditions 2023-01-19 18:56:02 +01:00
endpointslice_test.go Add support for EndpointSlice conditions 2023-01-19 18:56:02 +01:00
ingress.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
ingress_adaptor.go Kubernetes SD: Fix golint issue 2021-08-24 17:30:33 +09:00
ingress_test.go Implement target discovery in own k8s namespace 2021-12-14 13:41:38 +01:00
kubernetes.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
kubernetes_test.go Improve reliability of Kubernetes SD tests (#10761) 2022-06-07 16:19:40 +01:00
node.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
node_test.go Kubernetes service discovery: add provider ID label (#9603) 2021-12-06 22:27:11 +01:00
pod.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
pod_test.go Add container ID as a meta label for pod targets 2023-01-11 11:44:36 -05:00
service.go lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
service_test.go add loadbalancerip to service labels 2022-08-10 12:40:11 +02:00