Always encapsulate multi-line column statements in parentheses
Luttik opened this issue · comments
Search before asking
- I searched the issues and found no similar issues.
Description
I often see mistakes being made that are related to the legibility of case statements. Mainly because the default formatting results in a formatting where the case is broken within the statement
(before end
) and then has the as column
statement on the same line. Encapsulating the case in parentheses provides some visual space and prevents errors.
Going from:
SELECT
CASE WHEN species = 'Cat' THEN 'Meow' ELSE 'Woof'
END AS sound
CASE WHEN species = 'Cat' THEN 'Scratch' ELSE 'Dig'
END AS act
FROM mytable
To:
SELECT
(
CASE WHEN species = 'Cat' THEN 'Meow' ELSE 'Woof'
END
) AS sound
(
CASE WHEN species = 'Cat' THEN 'Scratch' ELSE 'Dig'
END
) AS act
FROM mytable
Another option which would not require parentheses, but also improves legibility, is the option to force when / else statements to always be on new lines. This would result in something that is more legible than the current implementation, but less legible than the parentheses option.
SELECT
CASE
WHEN species = 'Cat' THEN 'Meow'
ELSE 'Woof'
END AS sound
CASE
WHEN species = 'Cat'
THEN 'Scratch' ELSE 'Dig'
END AS act
FROM mytable
Use case
I'd like to achieve higher legibility when working with case commands.
Dialect
Not related to a specific dialect.
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct