aspose file tools*
The moose likes Meaningless Drivel and the fly likes tabs vs spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Other » Meaningless Drivel
Bookmark "tabs vs spaces" Watch "tabs vs spaces" New topic
Author

tabs vs spaces

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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/]


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

I strongly believe that code should be seen as the original author intended rather than as the viewer wants it.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3433
    
  47

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?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I would vote for that formal guy
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4634
    
    5

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

Joined: Aug 11, 2007
Posts: 4634
    
    5

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Spaces.
For Clojure I use Sublime Text 2 . Indent Using Spaces && Tabs width == 2

Displayed as intended on Github.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
Hmm my editor automatically indents correctly depending on the scope level, after I hit enter.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
134

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

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"


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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
Dan Ullom
Greenhorn

Joined: May 12, 2012
Posts: 1
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 988
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Yup, anyone mixing spaces and tabs should be beaten.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Bear Bibeault wrote:Yup, anyone mixing spaces and tabs should be eaten.

Mmmm... pass the chianti!
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2051
    
  22

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Oct 01, 2001
Posts: 2771
    
  10

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Nope. Get out the 2x4s!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
2 spaces please - hold the mayo...

One of my pet peeves is unintended line wrapping - makes my brain hurt...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Oct 01, 2001
Posts: 2771
    
  10

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12



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"
 
wood burning stoves
 
subject: tabs vs spaces
 
Similar Threads
XPATH vs tree parsing
package vs inheritance
tab vs. excape sequence "\t" in String interal
Formatting Java Code
Coding standards