Hacker News 中文摘要

RSS订阅

制表符与空格之争落幕,空格胜出 -- The Tabs vs. Spaces war is over, and spaces have emerged victorious

文章摘要

编程界长期存在的“制表符与空格缩进”之争已告一段落。通过对主流编程语言默认缩进方式的调查,发现大多数语言倾向于使用空格作为缩进标准。尽管个人偏好仍存差异,但数据表明空格已成为主流选择,为这一争论画上了句号。

文章总结

标题:Tab与空格之争:战争已结束

主要内容:

在编程界,关于代码缩进应该使用Tab还是空格的争论由来已久,但如今这场“缩进战争”似乎已经有了明确的赢家。通过对主流编程语言和标记格式的默认缩进规范进行调查,发现大约90%的主流语言和格式倾向于使用空格作为缩进方式,而Tab的使用则相对较少,仅在某些特定语言(如Go、Odin、Hare和汇编语言)中被明确推荐或依赖。

例如,Go语言的核心开发者通过工具强制使用Tab进行缩进,而不是仅仅依赖风格指南,这在一定程度上消除了使用空格的可能性。然而,大多数开发者和风格指南更倾向于使用空格,这反映了标准化的大趋势,类似于全球向公制系统或右行交通的转变。

尽管在某些特定领域(如航空或航海)中,英制单位仍然占主导地位,但全球范围内已逐渐形成了共同的标准。同样,大多数开发者和风格指南对空格的偏好表明,对于大多数实际应用场景,Tab与空格之争已经基本尘埃落定。

因此,无论Tab和空格各自的优缺点如何,这场“Tab与空格之争”似乎已经结束,空格成为了胜利者。然而,正如技术中的任何文化选择一样,问题依然存在:这场战争真的结束了吗?

评论总结

评论主要围绕“Tab vs. 空格”的争论展开,以下是主要观点总结:

支持Tab的观点:

  1. 灵活性与个性化:Tab允许开发者根据个人偏好设置缩进宽度,适应不同的视觉需求。

    • 引用1: "The point of the 'tab' option is that there is no consensus on the number of spaces to use -- so by using tab, one uses a single character, and allows the developer to determine screen spacing to their comfort."
    • 引用2: "Tabs are better, simply because you can view them as you like."
  2. Go语言的实践:Go语言使用Tab作为缩进标准,展示了Tab的实用性。

    • 引用1: "Rob Pike on why Go is indented with tabs: By making the indent be a tab, you get to decide the answer to that question and everyone will see code indented as wide (or not) as they prefer."
    • 引用2: "Thankfully all the go code I'm working with has tabs."

支持空格的观点:

  1. 一致性与维护性:空格在代码中更易于维护,尤其是在混合使用Tab和空格时,空格更一致。

    • 引用1: "Using tabs for indentation inevitably leads to a mix of both - which is, objectively, worse for maintenance and consistency."
    • 引用2: "I have changed my mind to using SPACES now because the editor lets me fake using TABS."
  2. 现代编辑器的支持:现代编辑器可以模拟Tab行为,使用空格也能达到类似效果。

    • 引用1: "More importantly, the mass of people who all coalesced on using the same editor in the web development sphere, Sublime then Atom then MS Visual Studio Code, has made it easier to just say 'set your editor to do this'."
    • 引用2: "Any modern text editor should allow you to write a simple plugin to display tabs or any combination of leading spaces as whatever indentation amount you prefer."

中立或实用主义观点:

  1. 自动格式化工具:使用自动格式化工具(如go fmt)可以避免争论,确保代码风格一致。

    • 引用1: "The most important thing is consistency. I've become a huge fan of deferring tabs vs. spaces debates to an autoformatter."
    • 引用2: "Just gimme a go fmt or mix format. These discussions are so 2007!"
  2. 编辑器与语言的默认设置:遵循项目或语言的默认设置,减少不必要的争论。

    • 引用1: "Projects should just pick whatever standard is most popular for their given language, and then people can tweak their editors for personal preferences."
    • 引用2: "The war is mostly over because regardless of it you use tabs or spaces, the many modern languages use a code formatter and reformat to whatever is common."

其他观点:

  1. 历史与演变:Tab和空格的使用随着编辑器技术的发展而变化,现代工具已经模糊了两者的界限。

    • 引用1: "But somewhere along the way, editors added features that let you see invisibles, and let you set up smart tabs so that you could hit tab, but it would interpolate 4 spaces (or whatever you set) into the document."
    • 引用2: "On the other hand editors often now handle spaces as if they are tabs, even for cursor movement. So well. Both won?"
  2. 语义与实用性:Tab的语义价值在现代IDE中可能显得多余,空格更直接。

    • 引用1: "The original 'tabulator' key was intended to save time and effort aligning text with spaces. With the ubiquity of decent IDEs that objective is unnecessary."
    • 引用2: "Words can become obsolete if there are other more commonly understood ways of expressing the same thing. This seems similar."

总结:Tab和空格的争论仍在继续,但现代工具和自动格式化器的普及使得这一争论逐渐失去实际意义。开发者更倾向于选择一致性强的方案,或依赖工具自动处理。