Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

tabs vs spaces

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A colleague and I recently debated using spaces vs. tabs for indentation. We both agreed with the sentiment behind this



I know some people around here have strong opinions...I'd like to hear them.

I don't care HOW MANY spaces you use, I am only interested in whether you think it should always be a tab, or always be spaces, and why.

[edit - image taken from http://evadeflow.com/2011/03/tabs-vs-spaces-in-the-21st-century/]
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spaces -- that way the code always looks as the original author intended. Tabs can make the code look all wonky if the settings aren't exactly the same as the original author's.

Most IDEs can be set to insert spaces when the tab key is pressed, so the usual argument that tabs are "better" because they need fewer keys-strokes is irrelevant.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nowadays I always use spaces, for the reason Bear gives, and because I get a smoother experience when viewing the same file in different editors (I've never quite got the patience to configure them all exactly as I want them).

However, I used to work on projects with a colleague who had different (and equally strong) preferences for the number of spaces as me. We agreed to use tabs for everything, so we would each see the code as we wanted it, and had no difficulties. We wouldn't have been able to get the same effect with spaces.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I strongly believe that code should be seen as the original author intended rather than as the viewer wants it.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I strongly believe that code should be seen as the original author intended rather than as the viewer wants it.


Yeah, but the problem was "the original author" was often both of us. Hence that wasn't possible without inconsistent formatting. Or one of us compromising, and that was never going to happen .
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One argument raised by a peer...

We have tons of tcl scripts. Each time they are called, they have to be compiled - similar to perl.

He says that the compiler has to deal with each space character (2, 4, 8...) vs one tab character. Therefore, it is faster for the compiler to deal with one.

On any given day, we process 25,000,000 messages. Any single message may result in anywhere from 5 to 40 scripts being called. He claims that adds up...

I think he is micro-optimizing...I can't imaging parsing spaces takes any significant time, but I don't have any facts to counter him.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Or one of us compromising, and that was never going to happen .


Well, that was the problem -- not tabs vs spaces.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I think he is micro-optimizing...I can't imaging parsing spaces takes any significant time, but I don't have any facts to counter him.


I'd say he's the one who needs to supply the facts.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:On any given day, we process 25,000,000 messages. Any single message may result in anywhere from 5 to 40 scripts being called. He claims that adds up...


Why not stop indenting anything at all and using single letter identifiers throughout all the scripts then? And also definitely strip all comments out. Yes, I know. But in this forum... :-)

Even if he was true, wouldn't it be better to maintain the scripts in readable form and just process and obfuscate optimize them for the compiler automatically during the deployment?
 
Saloon Keeper
Posts: 15657
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always spaces. Reasons have already been mentioned, and I agree strongly with Bear's point that code should look like the author intended.

How much spaces are used for the indentation is pretty much a non-issue, but what I do think is important is that I sometimes want different lines of code to line up in a certain way, so it makes it more obvious that they are related. Spaces are the only reliable way to go there.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would vote for that formal guy
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I think he is micro-optimizing...I can't imaging parsing spaces takes any significant time, but I don't have any facts to counter him.



You *think* he is prematurely nano-optimizing something that happens occasionally?
What facts would you need to make you be sure that you and I and Bear are right?

Anyone who thinks that the time processing whitespace is significant in the compilation process has never looked at the internals of a compiler.
 
Pat Farrell
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no religious problem with the IDE converting tabs to spaces. I like typing tab, because its faster than typing four spaces. For a low, low fee, I can be convinced to change my mind.
 
Master Rancher
Posts: 4934
75
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I strongly believe that code should be seen as the original author intended rather than as the viewer wants it.


Nah - I believe the code should be seen as I intend it to be seen. Regardless of what the original author or other viewers want.

Well, seriously, most code has enough different authors; I don't care much about "original intent". I just want to to be consistent. If I saw more original authors who were capable of that, I would probably pay more attention to what they wanted. But that sort of thing varies from company to company; currently I'm in a bit of a Wild West anything-goes environment, and that colors my thinking.
 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spaces.
For Clojure I use Sublime Text 2 . Indent Using Spaces && Tabs width == 2

Displayed as intended on Github.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:I like typing tab, because its faster than typing four spaces.


The free editor i found has a flag that automatically converts a tab to X spaces. So, I too hit tab, but get the spaces I personally prefer.

I can't imagine real full-blown IDEs are any different.
 
Stephan van Hulst
Saloon Keeper
Posts: 15657
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm my editor automatically indents correctly depending on the scope level, after I hit enter.
 
author & internet detective
Posts: 41944
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Bear Bibeault wrote:I strongly believe that code should be seen as the original author intended rather than as the viewer wants it.


Nah - I believe the code should be seen as I intend it to be seen. Regardless of what the original author or other viewers want.

Well, seriously, most code has enough different authors; I don't care much about "original intent". I just want to to be consistent. If I saw more original authors who were capable of that, I would probably pay more attention to what they wanted. But that sort of thing varies from company to company; currently I'm in a bit of a Wild West anything-goes environment, and that colors my thinking.


Well said. I don't care what people use so long as it is consistent and they don't reformat the whole file between commits obscure the fact that only one line changed.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:I don't care what people use so long as it is consistent and they don't reformat the whole file between commits obscure the fact that only one line changed.



Oh, so much this!

At one job back in the mid-90's, the tech lead had this thing about 3 spaces for indentation and he would reformat everyone's code, making diffs across versions impossible.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont need to waste my brain cycles (which might mean I need to be awake when I am working) over the tab/space choice.
My IDE is setup to automatically indent. We use a formatting template as a standard. The only key I really use (for formatting per se) is the return key.

It would be an interesting study to see if there is an co-relation between people who prefer the curly brace to start on the same/next line and tab/space. Totally useless study, but time consuming and in Dilbert's universe it would even pass off as "work"

 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
At one job back in the mid-90's, the tech lead had this thing about 3 spaces for indentation and he would reformat everyone's code, making diffs across versions impossible.


diff -w
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm the guy Fred, the OP, is referring to and I prefer tabs.

Tabs let everyone dynamically adjust how visually deep the indentation is. Two people can view the same code in the way that makes the most sense to them, thus improving understanding and efficiency. I don't care what makes sense to other people, it needs to make sense to me. We all just need to set a tab stop that makes sense for us.

Even the best IDEs don't mask the use of tabs/spaces all that well.

I want # Tabs == # Backspaces.
I want # Tabs == # arrow key presses (for navigation and text selection, this is critical).

For these reasons, tabs currently give me the best editing experience. I move my cursor with the arrow keys a lot and I would argue that tabs result in greater productivity than spaces, especially when not using a modern IDE.

The real problem for Fred and me is that our team does not indent code in any consistent way. I can deal with spaces. I can't deal with improper indentation.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spaces! I agree with Bear. Mixing them up is worse than consistently using either spaces or tabs.

fred rosenberger wrote:On any given day, we process 25,000,000 messages. ... I think he is micro-optimizing...I can't imaging parsing spaces takes any significant time, but I don't have any facts to counter him.


The time and energy he spent thinking about this "problem" is most likely larger than what he would save with micro-optimizing.

Pat Farrell wrote:I have no religious problem with the IDE converting tabs to spaces. I like typing tab, because its faster than typing four spaces. For a low, low fee, I can be convinced to change my mind.


I also like typing tab, but when I type tab I actually get four spaces. Win-win!
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Spaces! I agree with Bear. Mixing them up is worse than consistently using either spaces or tabs.


OK, but that was Fred and Dan's original point, which essentially everyone agrees with.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few things:

1.

Bear Bibeault wrote:Spaces -- that way the code always looks as the original author intended.



I agree as long as the original author was relatively sane.

The biggest exception I've seen to that was a programmer that used one space between indentation levels. Apparently he developed that practice because he often had if/else blocks and for loops so deeply nested that horizontal space became an issue.



In such a case, is "as the original author intended", worth keeping?

2.
Yes, spaces will make multi-line comments work out nicely.



They also come in handy when creating certain types of long expressions.



IMHO that last example is much easier to read and understand than the one-line equivilent:




If everyone uses spaces, then that code will line up for everyone always. On the other hand, if I coded that in my IDE with a TAB set to four spaces and then someone looks at in Notepad, where TABs are set to eight spaces, it would look like...




(And yes, I'm a fan of unnecessary parentheses, such as the ones around the 'a' in the first version, and spaces, such as the ones that make the 4 and 8 line up with the 12 correctly.)

 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, anyone mixing spaces and tabs should be beaten.
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Yup, anyone mixing spaces and tabs should be eaten.


Mmmm... pass the chianti!
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In an ideal world, both. Wait, though! Put away those blackjacks and baseball bats! Spaces don't work well for indents unless your whole team agrees on how many spaces an indent should be. Tabs don't work well for multi-line constructs, whether those are comments or long lines of code, because other editors or printers may interpret the tabs differently and screw up the alignment. What we should do is use tabs for indents, but spaces beyond the indents to align continuation lines. However, everyone would forget to do that, including me, so in practice we'd just get a dog's breakfast. Therefore, the best option is to have your editor set to automatically convert tabs to an agreed upon number of spaces. (Hint: the right answer is 4, at least for Java code.)

Actually, an ideal world wouldn't have computers at all.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We use code formatter that replaces all tabs with 4 characters. Also, we setup an SVN hook that uses the code formatter with the same rules file to check if you formatted the code. You didn't format code before checkin? No checkin for you!
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:In an ideal world, both. Wait, though! Put away those blackjacks and baseball bats! Spaces don't work well for indents unless your whole team agrees on how many spaces an indent should be. Tabs don't work well for multi-line constructs, whether those are comments or long lines of code, because other editors or printers may interpret the tabs differently and screw up the alignment. What we should do is use tabs for indents, but spaces beyond the indents to align continuation lines.


Um, assuming this wasn't just meant in jest... how would that work? How many spaces would you use for a continuation indent, and how do you know that length isn't the same as a tab in some user's configuration, causing confusion?

I prefer that everything be indented with spaces, and continuation indents are twice as big as standard indents. So if I'm working at a place where a standard indent is 4 spaces, then a continuation is 8. Or if the standard is 2 (my preference), a continuation is 4. Either way, there's no confusion between a continuation and a standard indent.
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, no jest. I think you just didn't read it carefully. I suggested using tabs for indentation and spaces for alignment. If you just need to indent a continuation line, you could use a tab or (ugh) two. If you needed to align code, like in a couple of Ryan's examples you'd use spaces.



That way you could set your indents to two spaces (masochist!), and I could still avoid a migraine ... all without affecting alignments, and without reformatting code. Neat, huh? The problem is, that I'd inevitably forget and put tabs in where I have "spaces in here", and when someone else with a different tab settings viewed the code, they'd see an impossible to read mishmash.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. Get out the 2x4s!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 spaces please - hold the mayo...

One of my pet peeves is unintended line wrapping - makes my brain hurt...
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:No, no jest. I think you just didn't read it carefully.


How odd. I think I did read it carefully, and it seemed less than clear. Perhaps starting with the idea that "alignment" was somehow a completely separate thing from indentation.
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even the demonstration didn't distinguish them for you? Oh, well. I'll link you to their definitions once I give the slip to this angry Bear.
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg, the problem for me is that it seems there would be non-obvious corner cases. For example:

That second line might be indented 4 spaces because it's a continuation and I usually use four spaces for a continuation, as opposed to two for a regular indent, so I can easily tell the difference between a continuation and a regular indent. Or it might be because I am intentionally making the two conditions in my continuation "align". What if I do both? Based on what you've said, it sounds like I could represent this as either

(where each "T>" is a single tab character, and I have tabs set to 2 spaces)
or

Now if these are viewed by different people with different tab settings, they're going to see very different things, right? This seems like it leads to madness.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mike Simmons
Master Rancher
Posts: 4934
75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And maybe this also ties into where we put opening braces. If you do opening braces on a new line, then you don't need indentation to tell you if a line is a continuation or a new block. But if you put the brace at the end of the previous line (as I prefer) it's more important to have another clear visual cue as to whether a line is continued or the start of a new block. That's why I seek a clear rule in these cases.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:In an ideal world, both. Wait, though! Put away those blackjacks and baseball bats!


What?! Where's my bat! You should be forced to program a complex enterprise system in Whitespace!
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I think this is what they mean. every line has the 'correct' number of tabs. Each line/part of the really long if-conditions (lines 11-13 and 15-16) all have the same number of tabs, because they are all part of the if-statement. But to preserve alignment, you use spaces.

If you take the above code and paste it into an editor, you can change your tabs to whatever width you want, but the alignment is preserved. Each line has a few tabs at the beginning for the formal indentation. The, the really long lines (the if-condition) use additional spaces to line things up.

I guess the rule would be "0 or more tabs for indentation, followed by 0 or more spaces for alignment"
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic