TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Nobody talks about the real reason to use tabs over spaces

122 点作者 kbr将近 6 年前

22 条评论

hermitdev将近 6 年前
Personally, I prefer hard tabs to spaces. The rationale is simple: every single dev I&#x27;ve worked with has had a separate preference on how many spaces ahould be used for an indent. I&#x27;ve seen 2, 3 and 4 commonly used. If you use hard tabs, every dev can set their editor to their indentation of choice. If you use spaces, that freedom is lost.<p>I say this as a currently predominantly Python dev, where spaces are encouraged and recommended, but I disagree as stated above.<p>Currently, I prefer a 2 space indent after previously using a 4 space indent for decades.
评论 #20341862 未加载
评论 #20344872 未加载
评论 #20347934 未加载
评论 #20343673 未加载
评论 #20341652 未加载
hn_throwaway_99将近 6 年前
Not going to get into the details of the argument (there is a reason tabs vs spaces is considered the prototypical &quot;religious war&quot;), but I do strongly disagree with the <i>way</i> it was argued, and I felt the need to comment because I so frequently see this type of discourse and find it very disheartening. It&#x27;s what I usually call the &quot;If you disagree with me you want to kill puppies&quot; (or, in this case, &quot;if you disagree with me you hate blind people&quot;) form of argument. The author really isn&#x27;t inviting people to challenge his thinking with his final sentence - I feel like he&#x27;s instead trying to flat out invalidate other opinions by emphasizing how callous you must be to have them.<p>FWIW, I believe there <i>are</i> other opinions that are at least worthy of consideration, but I think other commenters have made those points as well.
评论 #20341724 未加载
评论 #20341802 未加载
评论 #20381986 未加载
swsieber将近 6 年前
Did you know that you can abuse font ligatures to have adjustable indents even when using spaces and not tabs?<p>Its possible using font ligautures to turn predefined sequences of characters into a different set of glyphs. This is usually used to display nicely joined letters, or rendering multi-character math symbols as a nicer single wide character. An example of the letters thing is the sequence &quot;fi&quot; on mac. It&#x27;s one I see on macs and in my android keyboard autocomplete.<p>But you can abuse it. I took a mac fixed width font and added a ligature that turned four spaces into three spaces. And what do you know, it actually worked. I tried it in IntelliJ and with whitespace highlighting on, it correctly takes up 3 spaces but has 4 space markings - so IntelliJ handles it properly.<p>I don&#x27;t advocate this, but i would like to make a cli tool someday that lets you take a ttf and add in whater spacing conversion ligature you want.
评论 #20342548 未加载
jchw将近 6 年前
The knee-jerk reaction is almost certainly going to go along the lines of “But you can emulate all of those features with spaces anyways!”<p>Which is true. But what if my tools don’t all do that? What if doing so makes it harder to work for some reason or another? Users who use this tab emulation will experience all of the downsides that spaces are supposed to avoid, like misaligning justified comments. I think it’s a waste of effort.<p>All I can say is, I appreciate the Go team’s decision to use tabs. It makes semantic sense, it puts everyone on the same playing field, and it doesn’t require editor support. Even Windows 98 notepad can be used comfortably as a code editor for tabs-based code, if you for some reason wanted to do that. In reality that is not necessary or desired, but it illustrates that this is the path of least resistance.<p>That web browsers handle tabs poorly is of no concern to me. If it were really a huge problem, pretty much any editor in existence can do a Replace All to get back perfect spaces-indented code.
评论 #20341692 未加载
评论 #20341706 未加载
评论 #20341772 未加载
评论 #20341712 未加载
frereubu将近 6 年前
One thing taught to me by an accessibility advocate in a university was that accessibility isn&#x27;t just for people who have sensory problems, Accessibility is about making things accessible for everyone. Given that everyone seems to prefer their own indent size, using tabs and setting the tab width in the editor is an accessibility win for everyone, not just partially-sighted users.<p>I&#x27;ve been a proponent of spaces until now because of consistency, but this argument has won me over to tabs.
alex_duf将近 6 年前
Any visually impaired person to join the debate and help clarify?<p>It seems everyone is giving their opinion except the affected population.
Grumbledour将近 6 年前
As a visually impaired person myself, I often find it funny that people who are zealots about certain topics suddenly change their mind because someone with a &quot;disability&quot; (Which can mean nearly anyone tbh) is affected. Its weird, how the other humans opinion becomes suddenly more relevant because I guess they pity them?<p>&quot;I will fight Frank to the death on tabs vs. spaces, but Marry wears glasses so she is always right!&quot; Humans are weird.<p>And to weigh in on tabs vs spaces, or rather not, while I always set my preferred tab with and use the tab key to indent, I don&#x27;t know what characters my tools insert and I don&#x27;t or ever have cared.
评论 #20342728 未加载
评论 #20343651 未加载
评论 #20342241 未加载
kazinator将近 6 年前
If editors respected logical indentation levels, tabs would be more feasible. Consider a pair of lines like:<p><pre><code> printk(KERN_INFO &quot;%s: blah, blah %d\n&quot;, __func__, arg) </code></pre> This will work okay like so:<p><pre><code> [tab][tab]printk(KERN_INFO &quot;%s: blah, blah %d\n&quot;, [tab][tab] __func__, arg) </code></pre> If we change the tab size, the alignment is preserved.<p>Problem is, many editors in &quot;mixed tabs and spaces&quot; mode wants to fill the second line with the maximum possible number of tabs, and minimum spaces. It has no idea that the two belong to the same nesting level. Say that we have four-space tabs. Then the editor does this:<p><pre><code> [tab][tab]printk(KERN_INFO &quot;%s: blah, blah %d\n&quot;, [tab][tab][tab] __func_, arg) </code></pre> four of our alignment spaces turn to a tab, followed by three spaces for alignment.<p>Even if your True Scotsman&#x27;s editor is smarter than this, someone on the team will use something that isn&#x27;t and mess things up.
scalablenotions将近 6 年前
So far, anyone who lives long enough becomes visually impaired. I hope to be able to still code when I&#x27;ve very old.
eterm将近 6 年前
What proponents of tabs over spaces miss is the fact that consistency is the most important thing.<p>Editors ship with spaces over tabs as default, so you should use spaces over tabs. You will be more consistent with others and reduce errors if people somehow haven&#x27;t picked up your configuration.<p>Anything else is just fighting the tooling.<p>Without consistency (hopefully brought by automation) you will find team members struggling with merge conflicts that were unecessary and only brought about because of whitespace changes.<p>Since we started forcing prettier on stage we&#x27;ve seen a reduction in manual merge resolution being necessary.<p>And if you&#x27;re using a tool anyway to apply this consistency, the decision just becomes the following, do you use the defaults or a custom configuration? Your life will be a lot easier if you just go along with the defaults, even if they aren&#x27;t your first preference.<p>That&#x27;s not to say you can&#x27;t custommise anything (e.g. the default line width of 80 in prettier is too low) but you should have a very good reason to do so. And as others have pointed out, this isn&#x27;t one of them because tooling can recognise spaces as tabs just as they recognise tabs and still adjust accordingly.
评论 #20341748 未加载
th0ma5将近 6 年前
I wouldn&#x27;t necessarily recommend jEdit, but I&#x27;m sure a lot of editors have a soft tabs feature like it. And with linters I can&#x27;t imagine you couldn&#x27;t just run your local fork with which ever have the integration make the production version be which ever. Anyway... I personally don&#x27;t care what you do so long as you try to do the simplest and least and obvious thing.
评论 #20341599 未加载
Glyptodon将近 6 年前
So one thing I&#x27;ve wondered for a while is why every single non-public tab-indented project I&#x27;ve ever had to join (as opposed to create) has been a disaster from a consistent code formatting perspective.
评论 #20342053 未加载
andy_ppp将近 6 年前
My take: whatever the project is currently using, spaces or tabs, please keep it that way. You might think this is important but really you can find arguments for either way, consistency trumps everything.
NikkiA将近 6 年前
I recently contributed to a GPL project that uses tabs+spaces, but I have emacs configured to not use tabs ever, at all.<p>After the first git commit it was obvious that this was going to be a problem, the diff lines didn&#x27;t match at all.<p>So I&#x27;m seriously considering, after 38 years of being a die-hard &#x27;spaces or death!&#x27; user to switch to using tabs with my tab-stop size configured to match my spaces preference.
quickthrower2将近 6 年前
I was a tabs guy but now I’ve made peace with spaces. Honestly any IDE including low touch ones like Vim will auto indent for you anyway.
amanzi将近 6 年前
I don&#x27;t have a strong opinion on this long-running debate, but the example given sounds like a valid argument.<p>I just changed my VS Code settings to not insert spaces when pressing tab (&quot;editor.insertSpaces&quot;: false) and will see how it goes. Being able to change the tab size (&quot;editor.tabSize&quot;: 4) already seems like a neat feature that will come in useful.
Grue3将近 6 年前
Well, Lisp code can&#x27;t be properly indented with just tabs. So tough luck, I guess? Why wouldn&#x27;t editors programmers use allow to customize indentation space size instead? So you could set up each indentation space = 2 characters and get huge indents. Or set it to 1&#x2F;2 character and get tiny indents. Seems easy to implement in e.g. Emacs.
评论 #20344490 未加载
评论 #20344035 未加载
Stratoscope将近 6 年前
What most of these discussions leave out is that you can&#x27;t just say &quot;use spaces instead of tabs.&quot;<p>You have to say <i>how many</i> spaces.<p>Is it 2? 3? 4? 8? How many?<p>If you use spaces, you are committing to say how many will represent one indent. And please take note of how your choice affects the visually impaired programmers from the Reddit thread. Or someone like me who finds monospaced fonts hard to read and proportional fonts much more legible.<p>But why do you even need to say this? Why should it matter how many spaces represent an indent level?<p>The only time it really matters is when you not only use indentation, you use <i>column alignment</i>, like this:<p><pre><code> myFunctionThatDoesStuff(someArgument, andThisCalculatedOne(anArgumentThatMeansSomething, anotherWordyName)); </code></pre> This style is mandated by many coding standards, including Google&#x27;s C++ standard. If you use an alignment-based style, you had better use spaces, or else you are in the &quot;tabs for indentation, spaces for alignment&quot; muddle.<p>What would happen if you abandon column alignment and use only indentation:<p><pre><code> myFunctionThatDoesStuff( someArgument, andThisCalculatedOne( anArgumentThatMeansSomething, anotherWordyName ) ); </code></pre> Here the parentheses are treated just like curly braces: if you need to line stuff up, don&#x27;t do it at whatever column things happened to fall on, do it by indentation only. That&#x27;s how we code curly brace blocks.<p>Now your code will be just as readable if it&#x27;s indented with tabs, or with whatever number of spaces you like.<p>If we coded curly brace blocks the way these standards require us to use parenthesized expressions, our code would end up looking like this:<p><pre><code> if(someBooleanExpression) {someFunctionCall(); if(anotherBooleanExpression) {doSomething(); doAnotherThing();}} </code></pre> Do you code like that? Do you <i>want</i> to code like that? Then why do you format code like that (if you do) simply because the delimiters happen to be parentheses instead of curly braces?<p>If you stop using column alignment, and start using indentation instead, so many problems go away. Space indentation (and how many spaces?) no longer has any advantage over tabs.<p>I sometimes wonder why so many coding standards mandate column alignment. I think it is because people get tired of talking about it and say &quot;we must agree on <i>something</i>, now! And we will stick with it forever.&quot;<p>I&#x27;ve been in these discussions, and what happens is whoever is most &quot;passionate&quot; about their particular style gets their way. And that will usually be the person who is fussiest about these things, the person who loves fiddly column alignment. Why else would such an impractical approach be baked into so many coding standards?
评论 #20348576 未加载
Stratoscope将近 6 年前
Meta: I am curious why this suddenly dropped off the home page and down to around #98. It can&#x27;t be the upvote-to-comment ratio, as there are 85 up votes and 43 comments. Was it a manual intervention?
评论 #20348422 未加载
scarejunba将近 6 年前
Has more energy ever been expended more often for less gain than this debate? It is truly the most well-painted of bike sheds.
评论 #20341755 未加载
mikorym将近 6 年前
I don&#x27;t see why people spend time on this. Someone mentioned recently (or maybe I dreamt this) that programming is really, really much more about the specification than the implementation. If your specification is done flawlessly, then the implementation follows from that. I had the idea a while ago of starting a alternative to Excel and then I realised it would be way more useful to simply <i>document</i> Excel&#x27;s behavior.
评论 #20341929 未加载
shitgoose将近 6 年前
click click click click click click click click vs tab<p>sounds like a perfect embodiment of modern software development style.
评论 #20346275 未加载