prometheus/promql/parser
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
..
ast.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
functions.go promql: Add histogram_count and histogram_sum 2022-06-28 18:16:48 +02:00
generated_parser.y labels: simplify call to get Labels from Builder 2023-03-22 17:05:20 +00:00
generated_parser.y.go labels: simplify call to get Labels from Builder 2023-03-22 17:05:20 +00:00
lex_test.go Run gofumpt on all files (#10392) 2022-03-03 17:21:05 +01:00
lex.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
parse_test.go Update tests 2023-03-08 16:32:39 +01:00
parse.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
prettier_rules.md Adds support for prettifying PromQL expression (#10544) 2022-07-07 18:13:36 +05:30
prettier_test.go Prettifier: Add spaces with non-callable keywords (#11005) 2022-07-15 00:09:56 +02:00
prettier.go Adds support for prettifying PromQL expression (#10544) 2022-07-07 18:13:36 +05:30
printer_test.go Prettifier: Add spaces with non-callable keywords (#11005) 2022-07-15 00:09:56 +02:00
printer.go style: Replace else if cascades with switch 2023-04-19 17:22:31 +02:00
value.go promql: use explicit type declare instead of string. (#7716) 2020-08-02 09:57:38 +01:00