Why spaces are better than tabs




















Daenyth - perhaps if you are using a curly-brace language. Everyone can set it to his taste and be happy. Show 8 more comments. Tabs Now, of course, consistency matters more than either one, and a good IDE makes the differences negligible. That said, the point of this thread is to be a holy war, so: I prefer tabs: They're a character specifically meant for indentation They allow developers with different preferences in indentation size to change how it's the code looks without changing the code separation of data and presentation for the proverbial win!

It's impossible to half-indent something with tabs. So when you copy code from some website that used 3 spaces into your 4-space indented file, you don't have to deal with misalignment. What about copying that 3-space indented code into your tab-indented file, which you have set your tabs to 3 spaces? When you send your code to your compatriots they wonder why that function has such bad indentation! Tabs are not originally meant for indentation, they are meant for tabulation , and do a dreadful job at it Timwi: Wikipedia seems to say that they are meant for moving the cursor to the next of a set of fixed columns.

Doesn't sound like tabulation was the primary function. Also, I was referring to the post-typewriter era. Check for yourself. Another benefit of this approach - fewer keypresses when using arrow keys for left-right navigation. Show 13 more comments. Peter Boughton. Yes yes yes yes yes. One hundred percent yes. This lets everyone set their own tab width while preserving tabular formatting.

If you're worried about keeping within 80 columns as you probably should be! Honestly, is this so difficult? Yes, but don't use spaces to line up within a line. Your colleagues may be using a proportional font so your ASCII art is going to look weird on their screens. Add a comment. Tabs allow mistakes to be more noticeable , it's annoying to micro manage to spaces.

Spaces should be used where tabs are completely useless. Perhaps that you need to upgrade :p? MaR: Noticable mistakes is true, the width of a tab is larger.

Tabs are consistent as a single Tab is an indentation, while you otherwise have to define how many spaces you will use for an indentation. Customization is an advantage. You are using tabs in the first place, so it's more productive.

Seems you can't even give a good reasoning why it is impractical, haters gonna hate TomWij: How is tab larger than equivalent number of spaces? Python is virtually the only language that got whitespace right to use tabs safely but still - it offers only little advantage compared to "insert spaces".

MaR: Where did I state 'equivalent number of spaces'? I've tried all those, tabs work perfectly in them! All of the arguments for tabs are great in theory. In theory, practice and theory are the same. In practice, they are not. And in an ideal world it would In reality, you can't see the difference between spaces and tabs, they always seem to get mixed up when moving code around, and viewing the code in another program that has tabs set to 8 columns is a nuisance. Every editor has a "show whitespace" option now, and Visual Studio has a 'tabify' option — Chris S.

Good argument. That's the reason I gave up on trying to convince people that tabs are more logical. Even programmers who should be used to abstract thinking seems to confuse tabs and spaces "if it looks like four spaces, then it must be four spaces!

The nail in the coffin for me was that it seems to be impossible to configure Emacs to indent only with tabs. It is too easy to silently do the wrong thing with tabs. I used to use tabs too, but I was running into trouble with some IDEs that couldn't insert spaces for continuation lines and I hated to replace all those auto-inserted tabs with spaces Visual Studio was one of them. So I just sticked with spaces. Some relevant opinions that may be of interest: Guido says spaces Joel says spaces Atwood says spaces Zawinski says spaces, sort of.

Just because you're famous doesn't make you correct. Xepoch: have you read their rationale :? Matthieu M. I don't like olives either but doesn't make me wrong because others do. I find it ludicrous that people wouldn't use the tab for indentation, but this is why they call them CS holy wars Xepoch: yes, I suppose everyone is entitled to one's opinion : I do think though that Guido has pointed the main issue: mixing tabs and spaces is evil, because both appear as "blank space" to the user.

Fishtoaster: When you say that Joel says spaces, shouldn't the link be to an actual Joel quote? The link just goes to the JoelOnSoftware forum where some ppl most likely not speaking for Joel discuss tabs vs spaces.

Show 2 more comments. Tabs for the win. Joel Barsotti. Unfortunately I'm the minority. Secondly, it gives you total flexibility in terms of choosing how big a tab is in your editor. Those who have incredibly nested logic can use 1 or 2 characters. Personally I always choose 4. The thing is, you choose how you want to see it.

The indentation will look the same always. If you want to ensure that people always see it the same, you simply specify in your coding standards that people should always set their indentation level in their editor to 4. The advantage of a tab is that it allows you to choose your visible indent size.

However, varying indent size makes it functionally impossible to enforce column width. Therefore if you have a coding standard with a fixed width, spaces are the appropriate choice for justification.

Why would you need to enforce a column width though? Alignment after indentation? By column width, I meant the total document width. If a document has a column max width, and one person uses two space tabs, and another uses four space tabs, and another uses eight, and they all use a character width, then when one person views the work done by another, then the formatting will look wrong.

Alignments say, for simple ascii images are going to be off too. You want to use spaces in your code? Their code could look messy and take longer to walk through. The main advantage as I see it is that everyone on a project seems the same code. Sometimes a previous version of myself.

It solves the multi-programer problem without sacrificing any principles. I wish more programming languages would supply such tools code formatting normalization. TISA is the best solution. If all developers on your team are using tabs, how would spaces benefit the team or its members? Thus, it can be impossible to distinguish between words separated with a tab and words separated with a single space. Though, I acknowledge that some modern editors will display a symbol for a tab, which solves that problem.

Due to the nature of code, lining things up with one tab setting may look awful in any other tab setting, thus making the code harder to read for some, easier for others. You can make the argument you should never attempt to neatly line up code, but there are sometimes good reasons to do so. Can you work around all these issues with tabs, or simply live with these edge cases? Yeah, probably. Most people I know who use tabs only use them at the beginning of the line. The correct answer is tabs for indentation, spaces for alignment, and alignment is generally to be discouraged.

The problem with spaces for indentation has exactly the same foundation as people are complaining about for tab width: one developer wants to use 2 spaces per indent level, the next wants to use 4. We can certainly agree that if everyone always uses tabs consistently and everyone always uses the same editor configured in the same way, then tabs will work fine, and even allow people to use different indentation widths.

Once you have a mixture of tabs and spaces, the code will start looking weird for someone, not necessarily you. My favourite example is when you look at code written by tab fans who configure their tab width to say 4 spaces. I mean really, that argument could be applied anywhere where there is any sort of debate on how or what to use — package managers, specific packages, etc.

Or am I missing something? Now, to your question: The thing is, if everyone uses only spaces or only spaces and 8-space tabs, it mostly works everywhere because all editors and viewers understand spaces well, HTML interpreters ignore them unless in pre tag, but… and many viewers also handle 8-space tabs okayish. Now someone figured out that you can use tabs as a sort of system, just like when tabulating data.

The benefit is it enables people to configure their favourite tab width. And some people do that, and THEN we have the problem because suddenly we have non-standard tabs so the file is going to look different to everyone else than the author and people with editors configured like the author.

Worse, the code can end up being inconsistent, unnoticed, and then it can garble up completely for other people. The only way to fight that is to start being careful about the tabs. You seem to think that you can standardize everyone to use tabs. And there are still downsides. My life was simpler when I decided jwz was right and I made my editor not insert 8-space tabs, navigation became much more intuitive. Now this is an argument, mostly against inserting non-standard spacing in text that other people may have to look at.

For example, in yaml, you have to use spaces because people with 8 space tabs could be confused with those who use 2 tabs with 4 spaces each. If you colleagues use 2, 4, or 8 space tabs code will not be aligned for everybody. Only using spaces guarantees it.

Readability trumps trying to save bytes. You use tabs at the start of the line and everyone can use whatever tab size they prefer. Especially with the ever-widening gap between how few bytes code requires vs how much bandwidth and storage is available.

Uh, no. This is the beauty of tabs. The main advantage of using spaces is that they always display the same way. I know that those who praise tabs use the same exact argument as a disadvantage or rather the point that tab can be displayed according to ones preference but this argument is invalid.

There is plenty of places in which such a setting cannot be set. GitHub has quite narrow, not configurable code view. Tab is displayed as 8 spaces. Especially inconvenient for side-by-side view of a diff. Age is likely to track with salary. The reason spaces are encouraged when training coders has to do with printing on paper. So older developers will have a different habit then newer developers.

Readability concerns are valid on screen just as much as on print. Interesting hypothesis. Never once did I think about the ramifications of printing when thinking about spaces or tabs.

Nor can one find a tendency where developers with the same years of experience that used tabs tended to be younger. In general, older employees do have a higher salary floor. However, developers with the same level of experience most likely trained for it around the same time, so they very likely learned the same habits.

If age was a primary factor in the pay gap, I would expect that at the very least, the older languages would stand out in the regression chart. Might be harder to transition to an open source stack because no one has been willing to pay me to work in one, but I am familiar with several.

To me it seems like blatant ageism to assume because someone has been in the industry for 20 years they favor or are currently using old languages. That was not my intent, I apologize. To be fair, something like Assembly would probably be more like embedded systems programmers. So it may already be a disproved hypothesis. Writing this comment made me hate myself.

I had a coworker who did essentially this. You can set your editor to automatically translate all tabs to spaces. Sure he will notice. He will likely set his editor to expand tabs to wrong number of spaces, given how many possibilities there are, and all the code will look fugly.

Spaces cause problems when reusing code,. Besides, spaces are generally much more variable than tabs in different fonts, I mean considering you get the whole fixed versus variable width fonts into the argument.

I mean I agree with you, copy and paste of code provided you wrote it, trust its author, or at least read it thoroughly is a pretty normal thing to do. But still, maybe pick a year? What makes you think copying and pasting is wrong? Well, that statement was not meant to insult you. Yes, sometimes we have boilerplate code which we copy as a template, like a default HTML file.

But everytime you copy code you risk that you have to fix a bug in that code in two places at a later time. Copy-pasting is not always bad, but copy-pasting from Stack Overflow day in and day out is a deadly, deadly trap. I copy and paste code all day along.

It saves me hours of time. The alternatives are A spend way too much time reinventing the wheel, or B pointlessly retype it all out the exact same. Every time. With great power comes great responsibility, or something like that. Well, I am not sure if you get my point. I am not against code reuse. I say code reuse is great because it saves a lot of time. I am against duplicating code.

So if the code you need exists in a library, use that library. But please do not copy the code from the library, but instead load the library, so that you can easily update it if there comes a better version at a later time. I say copy and paste as much as humanly possible…. I think we are both agreeing on that. Replace leading space with tabs, or replace leading tabs with space. What is the problem?

Oh my, can you imagine? I strongly prefer spaces, but I would rather use tabs than listen to colleagues whack away at their space bars all day.

Like really? Come on guys and gals. I can setup my tabulations to appear like 2 spaces without having it to be two spaces, which would be 2 characters instead of just one, making all my files larger. That applies to almost any IDE for the past 40 years. Kind of an interesting situation with Go. A Go user might not only program in Go though, and would indicate tabs. But for the purposes of this analysis they still have a set of answers either way for users who also said they use Go.

I took a quick look at the data 30 minutes and found the following insights regarding Tabs vs. When you split the group into younger and older programmers not by age but by experience , into the ones with up to 10 years experience Group A: and the ones with more than 10 years Group B: you can see the following:. As Programmers with up to ten years are not only novice programmers, but the ones with a more modern education, this leads me to the conclusion, that it is the result of a modern programming style which favors tabs.

So my conclusion is that space users probably earn more money as they have more experience, but the trend shows that the new generation tends to favor tabs and that there is already a majority of users who use tabs. You say that space users earn more money as they have more experience, but the chart shows that developers with the same experience makes different amount of money depending on their tabs spaces preferences.

You are correct, my conclusion is wrong as I did not mean that they earn more money right now, but that they will earn more money in the future as there will be more tab users with more experience and right now the majority of tab users has less experience. So my hypothesis is that tab users will not switch their style when they gain experience but that they will stick with their style and that their group will mature over time and make more money by then.

I will correct that. I have about 9 years of coding and I am a tabber. My father has been coding for almost 40 years and he is a spacer. You hear him all day… bang bang bang bang on the space bar. I put my money on your hypothesis.

Did read, and I recognize the author recognizes that as well as did statistical effort to explore the dat. Still funny. Spaces is the right answer, no matter what. Most languages are multy-platform. Spaces looks same on any platform and this is not the case for tabs. Just accept it. With spaces though you have different developers who use different numbers for each level of indentation. With tabs one tab is always one level of indentation. The title of this blog is in no way shape or form clickbait.

Click bait would be:. Learn how he increased his salary with this one little trick! This is a brilliant study idea, but you should modify your result to only apply to the sampled.

I would have expected better from stackoverflow. There is no reason given in the article to explain why. Correlation is not equal to Causation. Yes, they even say that in their post. They have no idea why it is, it just is. It says Correlation is not Causation in the conclusion. Add a new question on to the survey for next year asking if developers limit character length of their lines to 80 characters. Then see if this correlates with space-use. They are to be exiled to a horrific world where the only language available is PHP and the only editor is ed.

If it were worse than PHP, they might give up. PHP is just barely viable enough to entice them to keep going, thus prolonging their suffering.

Both is not the same as mixed. Using both tabs and spaces is not the same: I use tabs for some languages, and spaces for others. I never mix the two in a single document. At Cratejoy, we use spaces in Python and tabs for Javascript and Less but we never mix tabs and spaces in a file.

I use both tabs and spaces, because I use tabs for some programming languages, and spaces for other languages — but I never mix them: never both in the same document. Mixing tabs and spaces implies using tabs and spaces within the same document and indeed, sometimes on the same line. I like think most people reading this would see it the opposite way and as a great example of meaningless correlation between what comes down to stylistic preferences in most cases.

It is my opinion that the title is the opinion. The data sample is flawed in that his sample was chosen in a way that has a misleading trend that fits the title of the article to make it look like the correlation is the cause, when the statistic says more about his sampling techniques than it does about who gets paid more.

For example, only people who knew about this survey or visited a certain site or subset of sites were surveyed. Er, what sampling techniques are you referring to, exactly? The sample is the set of people who took the Stack Overflow survey and answered that question. More experienced programmers: 1. The linear regression included both programmer experience and tabs vs spaces, so the effect exists even beyond programming experience. So while anyone who prefers spaces to tabs can take code with tabs and instantly convert it to spaces, or make the tab distance equal to two or four, or whatever spaces for readability with a single keypress, someone who prefers two spaces per indent level will have a harder time making code with four spaces per indent level readable to him or her.

Because you are an experienced programmer, you know how to set your IDE to do these tricks, but an inexperienced programmer does not or does not value doing so. The article makes no claims about the technical merits of tabs vs spaces. It just points out the existence of a correlation. I used to be a strong tab supporter. Want 4 spaces, 6, 8 spaces? Then I joined teams where the 2 were being mixed all over the place. It took me 5 years to realize the importance of this. Are you saying you literally press the space bar for indentation?

Haha, of course not. Some using actual tabs, others using spaces. A tab key gets converted to 4 spaces. So you get space characters from tab keys but that also behave like tabs on backspaces too.

This is especially nice for python, my go-to money maker. If you want tabs to look like 4 spaces, you can set that. If 2, you can set that as well. He discovered that was a problem in files that mixed tabs and spaces because how your IDE renders tabs might differ from how many spaces someone inserted. If everybody uses spaces and not tab characters, the file will render the same for everyone.

My hypothesis: Somebody who touts their use of spaces is more likely to exaggerate their reported income. It sounds pretentious. Like, meanwhile, children are dying in africa, stop using your computer and do something about it. If your job is not in health, teaching or food making, you are useless, so you should stop wasting your time. Mah gawd you guys, who cares? If we put this kind of scrutiny on the SO data scientists, imagine what kind of scrutiny we could put on what YOU do all day, such as….

The majority of the devs on the frontier of new languages, and language research, use an editor like vim. To be sure. In 30 years, vi will 71 years old, and vim will be 55 years old. All of us are hoping for better, more functional tools in that timeframe. Everything else happens in another window at a shell prompt. When I need to make anything else more efficient, I update my command-line programs. Well, actually it depends on what you value.

If you want something feature rich, an IDE might be objectively better. If you just want to edit a few lines of source code, the small editor which is just an editor might be objectively better. Yeah right — like we need to memorize a zillion keyboard shortcuts to do the most obvious stuff!! Keeping up with tech is tough as it is, we need our memory for more important stuff. I suggest the ones using spaces might be largely the ones who believe they are better and have higher standards when negotiating a salary.

Which is not necessarily a bad thing. Developers who cannot extrapolate full solutions from limited information. Funny, a few days ago, I would have found unbelievable that someone would actually think of tabs as the tab key.

It also bends my mind that some people fail to understand that the ambiguity inherent in using tab characters is the very reason why space characters are strongly preferred by anyone capable of thinking logically. That ambiguity is a feature of tabs, not a bug. Those of us that like compact code with two space indentation can coexist peacefully with our coworkers that use 4 space tabs. As long as tab characters are in the code it is just a editor setting and both groups can be happy.

Even that one weirdo that prefers 8 spaces per tab can do his thing without bothering the rest of us. Fixed it from average to median. Though the argument still stands either way. The volume of low salary developers who cannot make the distinction brings down the median. Salary aside, I was a tabber for decades. And it was the predominant coding standard at many of my employers and clients. Then one day my friend and colleague Doug Y. That tiny little piece of wisdom was my Road to Damascus moment.

There is a bit of throwback in it. Tabs are faster to type, and IDE replacement is relatively new. Tabs are also a single character, and code files not having to count bytes is also relatively new. There is a lot of lag between the professional world moving past a paradigm, the educational world adopting that change, and the post-change students getting into the workforce.

How much relative? VB6 IDE is even smart enough to understand where the level of indentation ends and add only the correct amount of spaces…. I say this from experience both as a student coming out with 30 year out-of-date habits that I had to get rid of, and having trained several recent grads out of their school-taught habits that were based on green-screen limitations. But we are talking about the present and in the present, nobody is required to hit the spacebar 4 times for each indentation level.

Even if you are working with such an old language because you can use other editors instead of those IDEs. Contrary to popular belief, you can learn about obsolete things. My high school was teaching us to use computers on green screen terminals in the late 90s. I also worked as an adjunct professor for a while, and as part of the IT staff for a college, and with IT professors from 5 other universities. As for the present, no one is sitting over your shoulder telling you to change your typing habits.

Yes, nobody needs to be over your shoulder telling you to change your typing habits, because it is not needed anymore. Programmers that get paid more are more likely to be required to use spaces. I have never worked at a large company with a coding standard that requires tabs over spaces. I have used tabs only during the course of my graduation where everything works and looks good.

The real world requires you to use spaces. People using tabs work at larger companies and therefore make more money. And of course that was a typo…. Company size seems to have been controlled for though.

So, developers who know the difference and care about the NEXT reader of their code use spaces to preserve their formatting across tabstop changes. Most people in office environments work with a mandated IDE, eliminating the tabstop change question. That said, companies that allow diverse environments or working from personal equipment might pay more but require spaces for that reason.

This is all self-reported, yes? What if the conclusion is spaces developers are just inflating their salaries by 8. I think the survey could have been poorly developed. Those in the beginning of their careers might not understand or even know that the IDE can change the tab key output to spaces, which could explain the lower salary.

They tend to only do what they are told. My theory is that creative people tend to earn more and changing the default configurations could have correlation in this data. Or they could know what the IDE configuration is, but assume the question is about which key they press. Also, there is no technical difference between using the key tab or the key space making it irrelevant for a survey, while there are technical differences between the character tab and the character space such as filesize and indentation of lines of code written in more than one actual text line or languages without a clear indentation pattern like SQL.

The only difference the actual character makes is in display across multiple IDE configurations. And that actually benefits tabs unless someone goes along typing spaces sort of like someone shoving fixed-width objects in the midst of a variable width HTML page. It is treated as a factor in the present and that is what matters for a survey about the present. Like I have nothing better to do all day long than correcting code that starts in columns that are not multiples of 4….

Most IDEs nowadays will have an automatic document formatter. Well, Notepad here symbolizes the most basic of the text editors. Now, there might be a need where you have to quickly edit a code file. If the code has been indented by spaces and you are trying to add a few lines in the code, you will have to use 4 times spaces for each level of indentation.

If the code had been indented through tabs, well.. Suppose you are using a 4-space indentation, then the total size of the file added by your indent chars will be 4 times more than using tab indents. Now, in a complex program indents can easily go up to 5 levels. Yes, obviously the real numbers for the industry would be different than this. And this comes at absolutely no cost.

Yes, I have seen code examples like this in the wild. A better example using tabs would be -. So these are my pro-Tabs reasons. The only pro-Space argument I see is the very first code example in this article. I hope this article will get programmers aware of why they should use tabs or spaces, whatever they feel like. This might not be the case with everyone. If this article triggered a change in your coding habit, do let me know in the comments.

Also if you have a feedback about the article or would like to add to it, just throw in a comment. Rather there is a psychological basis for writing programs in a conventional manner: programmers have strong expectations that other programmers will follow these discourse rules.

If the rules are violated , then the utility afforded by the expectations that programmers have built up over time is effectively nullified. Many people think standard is better than metric as well. Just because many or most people think something, doesn't automatically mean it is better or correct. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Objective reasons for using spaces instead of tabs for indentation? Ask Question. Asked 5 years, 8 months ago. Active 1 month ago. Viewed 9k times. Are there objective reasons for using spaces instead of tabs for indenting files as per PSR-2 standard, can someone provide: facts, references, specific expertise on which PSR-2 standard is based? Explanation on accepted answer: According to Farsides' answer: repositories thing may be an exact case of why spaces are in PSR-2 explained as indentation tool.

Improve this question. Community Bot 1 1 1 silver badge. Find my answer below, there are specific reasons behind this. Farside Yes, there are reasons for it. And the fans of tabs have their reasons to argue in favour of tabs.

Neither side is "right", both sides have their reasons for why they decided on one thing or the other. Note: I favour spaces as well for the same reasons you give. But you'll have to give concrete evidence that this is the reason spaces are favoured in the PSR spec for it to be a real answer; any meeting minutes you can link to? We are in middle of same discussion at work.



0コメント

  • 1000 / 1000