prometheus/util
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
..
documentcli Document command line tools 2023-03-13 14:20:55 +01:00
gate
httputil refactor: move from io/ioutil to io and os packages (#10528) 2022-04-27 11:24:36 +02:00
jsonutil Name float values as "floats", not as "values" 2023-04-13 19:25:24 +02:00
logging lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
osutil
pool
runtime util/runtime: use ByteSliceToString from golang.org/x/sys/unix in Uname (#11070) 2022-07-30 19:35:03 +02:00
stats Changing TotalQueryableSamples from int to int64 (#10549) 2022-04-12 01:22:25 +02:00
strutil Implement SanitizeLabelName and Full variant 2023-02-03 15:28:33 +00:00
teststorage Add a feature flag to control native histogram ingestion (#11253) 2022-09-14 17:38:34 +05:30
testutil lint: Adjust to the lint warnings raised by current versions of golint-ci 2023-04-19 17:10:10 +02:00
treecache style: Replace `else if` cascades with `switch` 2023-04-19 17:22:31 +02:00
zeropool Use zeropool.Pool to workaround SA6002 (#12189) 2023-03-29 20:34:34 +01:00