SQL 可读性 : commas better at the end or at the beginning of select list?

标签 sql select formatting comma readability

即使我认为这可能只是个人品味的问题,我想知道您更喜欢(以及为什么)在选择列表中使用逗号。

一开始:

select first_name
      ,last_name
      ,address
  from people

或者结束:
select first_name,
       last_name,
       address
  from people

我个人更喜欢在每行的开头使用逗号,尤其是当我处理比单列名称更复杂/长的值时。

我觉得这样做更舒服,因为我总是不明白新元素“开始”的时间/地点。

我很想知道你们是否有任何偏好(或特定场景)以便从多个角度查看这个主题。

最佳答案

它实际上以人类语言处理的方式很重要。 Joe Celko(SQL for Smarties 等书的作者)指出了这一点:

But be sure to end the line where the split occurs with language token that lets the reader know that there is more to come. This is why putting a comma at the front of the line is a bad practice; when you see a comma you know there is something coming after it. Likewise, when you see a semi-colon, you know it is a terminator. That lets the reader mentally close up that unit of code, “clear his buffers” and parse the next statement.



hackernoon 的 Felipe Hoffa 对前导逗号的使用进行了深入研究,发现强制使用它们的项目在 GitHub 上既不受欢迎也不​​成功:
https://hackernoon.com/winning-arguments-with-data-leading-with-commas-in-sql-672b3b81eac9

在前面使用 SQL 并没有错,但它不受欢迎并且会分散读者的注意力,因为它降低了语言的可读性。我不知道其他人,但是当任何标点符号(句点、逗号、分号)位于除括号或括号之外的行首时,我阅读代码并不容易。

关于SQL 可读性 : commas better at the end or at the beginning of select list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205891/

相关文章:

sqlite - SQLite 中的日期函数不选择数据库中的行

c - Astyle 错误地格式化 Linux 风格的大括号

mysql - SQL 检查一列中的重复项并删除另一列

c# - sql大数据读取

MYSQL - 一列引用多表

sql - 如何在选择查询中执行存储过程

sql - 如何有条件地选择 Oracle 查询中的列

javascript - 如何禁用选择框中的特定值之一

javascript - 无法将格式传递给 JavaScript

ruby - 如何将数字 1000 格式化为 "1 000"