• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Indenting in code - Tabs or Spaces?

Tabs or Spaces

  • Tabs

    Votes: 58 60.4%
  • Spaces

    Votes: 38 39.6%

  • Total voters
    96

jrbryner

[H]ard|Gawd
Joined
Feb 6, 2001
Messages
1,530
We're having a debate in the office. When you indent your code, do you use tabs or spaces?
 
Tabs set to soft tabs (tabs as spaces). People and applications just can't set their hard tabs correctly for whatever reason, so to save everyone the hassle of mixed tab environments, the standard at my job is to make all tabs = 4 real spaces.
 
It depends on the IDE or text editor you're using. In Visual Studio I always use tabs... because they end up being spaces. In vim I use spaces. In notepad I use spaces... and curse CRLF...
 
I use the tab key to insert 4 spaces.

I think I'd rather use actual tabs though, but don't because there are too many things that don't support a tab-display-width preference and code looks like crap when tabs are displayed at normal size.
 
spaces dear god, and set "soft tabs" -- tab = 4 spaces

ditto, with a but:

i generally code small project in perl or java, so i usually stick with vi and a standard 80-character-width terminal so i can be assured the code will be formatted correctly on most setups. as such, horizontal space tends to be a bit scarce so i usually use 2 spaces

it makes for some difficult reading sometimes, but it also keep me weary of writing unweildly nested loops so it works for the best since i can get a bit sloppy from time to time

if 120-character-width+ terminals ever become the standard then ill probably move to 4 spaces
 
I don't code with a fixed-width font (long story), so I use tabs. Spaces just don't take up enough space in variable-width fonts.
 
Visual Studio does it for me (and I'm perfectly fine with how it does it)
And fixed format RPG gets rather non-worky when indenting things.

But soft tabs (4 spaces) for the rest.
 
So, if everyone says spaces or soft-tabs WTF is tab in the lead?

You realize that, historically, the tabs/spaces debate is over using multiple spaces v. a tab character in your source, not which key you use?
 
So, if everyone says spaces or soft-tabs WTF is tab in the lead?

You realize that, historically, the tabs/spaces debate is over using multiple spaces v. a tab character in your source, not which key you use?

Wow, and spaces were up as of yesterday afternoon. I voted for spaces, don't blame me!
 
I don't code with a fixed-width font (long story), so I use tabs. Spaces just don't take up enough space in variable-width fonts.

Yes this is a good reason to use tabs. Fixed-width fonts are harder to read I find.

For coding though I use fixed-width and spaces because I'm almost certainly not the only one reading and writing it.
 
I use tabs. I don't want to force how much space an indent takes up. If one user wants it to look like 2 spaces, and another wants it to look like 4, that's their choice, I shouldn't be forcing it on them.
 
Soft tabs @ 3 spaces each.

It's just too annoying when someone tries pretty'ing up their code with tab characters. Invariably, everyone seems to have a different idea of how wide a tab character should appear, and the code winds up looking good only in the original author's editor and font.
 
Soft tabs @ 3 spaces each.

It's just too annoying when someone tries pretty'ing up their code with tab characters. Invariably, everyone seems to have a different idea of how wide a tab character should appear, and the code winds up looking good only in the original author's editor and font.

That's a problem with spaces, not tabs. I want my code to look how I want on my screen, and I want my code to look how other people want it to look on their screen. By using 3 spaces, it's 3 spaces no matter what.
 
That's a problem with spaces, not tabs. I want my code to look how I want on my screen, and I want my code to look how other people want it to look on their screen. By using 3 spaces, it's 3 spaces no matter what.

Exactly. The problem is that tabs are inconsistent, so if one goes about attempting to format their code with them, anyone trying to view the code must match the original author's tab widths in order for it to look correct. Spaces eliminate that concern.

Here's what I'm talking about.

Code:
       SomeFunction
              (elephants,      // Pachyderms
               zebras,                     //  Another mammal
               foo,                                   //  Another comment
               blahBlah,        //  Etc...
               yetAnotherParameter);   // More

       myRecord.fieldA                  =  a + b;
       myRecord.fieldFooBar  = c * d;
       myRecord.fieldEtc           = Function();

The above is not exact, but it's meant to illustrate what happens when code gets formatted with one tab width and then viewed by someone else with a different tab width. Whereas with spaces, you can format it exactly how it was meant to be read:

Code:
       SomeFunction
              (elephants,              // Pachyderms
               zebras,                 //  Another mammal
               foo,                    //  Another comment
               blahBlah,               //  Etc...
               yetAnotherParameter);   // More

       myRecord.fieldA       = a + b;
       myRecord.fieldFooBar  = c * d;
       myRecord.fieldEtc     = Function();

The only case in which spaces won't get the job done is where variable-width fonts are involved, and even then, tabs still won't allow for consistent formatting.
 
this is the case im generally more concerned about

Code:
for my $someOtherObject (@objectList) {
  if ($thisVariable == $someOtherObject->getVariable(something) || $thisVariable == $someOtherObject->getSomethingElse())
    do_something();
  }
}

Code:
for my $someOtherObject (@objectList) {
    if ($thisVariable == $someOtherObject->getVariable(something) || $thisVariable == 
$someOtherObject->getSomethingElse())
    do_something();
  }
}

letting people view code with their own settings is all sunshine and lollipops unless you know that they'll likely be using a fixed 80-column terminal to view your code and that a couple extra tabspaces will cause wrap-arounds everywhere.

and i wonder how many people have complained about the formatting of something else's code when the root of the cause was simply differences in tab-width settings

spaces are the best way to C.Y.A.
 
Tabs. I can't remember ever using an editor where tab spacing wasn't settable, so once we've settled on tabs, it's just a mater of teaching everyone to use the same tab stops.

It's really inconsequential in the long run, anyway. Your team has far more important things to worry about than this.
 
The question is indenting code, not lining up textual elements inline. These are 2 different issues. It is my opinion that tabs are the only correct tool for indenting code, and spaces for actually lining things up that are inline with code.

Exactly. The problem is that tabs are inconsistent, so if one goes about attempting to format their code with them, anyone trying to view the code must match the original author's tab widths in order for it to look correct. Spaces eliminate that concern.

Here's what I'm talking about.

Code:
       SomeFunction
              (elephants,      // Pachyderms
               zebras,                     //  Another mammal
               foo,                                   //  Another comment
               blahBlah,        //  Etc...
               yetAnotherParameter);   // More

       myRecord.fieldA                  =  a + b;
       myRecord.fieldFooBar  = c * d;
       myRecord.fieldEtc           = Function();

The above is not exact, but it's meant to illustrate what happens when code gets formatted with one tab width and then viewed by someone else with a different tab width. Whereas with spaces, you can format it exactly how it was meant to be read:

Code:
       SomeFunction
              (elephants,              // Pachyderms
               zebras,                 //  Another mammal
               foo,                    //  Another comment
               blahBlah,               //  Etc...
               yetAnotherParameter);   // More

       myRecord.fieldA       = a + b;
       myRecord.fieldFooBar  = c * d;
       myRecord.fieldEtc     = Function();

The only case in which spaces won't get the job done is where variable-width fonts are involved, and even then, tabs still won't allow for consistent formatting.
 
So if you set the spacing of tabs to be the same as everyone else, does that mean you are using spaces or tabs?

And if you set tabs tabs to equal x number of spaces, does that mean you are are using tabs or spaces?

It seems that people voting for tabs and spaces might have the same idea in mind, but vote opposite of each other because the poll is unclear.
 
pretty much everyone that uses soft-tabs has it binded to the tab key, but i suspect there's a few people mixing up the use of tab >characters< with using the tab >key< to generate spaces.

everyone has to deal with tab characters to some extent, so preferred settings for tab-widths arent really the issue, since one might still prefer spaces in their own code.

tabs are the obvious thing to use though, so the mismatch between the votes and the comments might be due to the fact that people who use spaces feel inclined to explain themselves, whereas everyone else just votes and moves on.
 
I use the tab key.
whether the thing inserts a \t or space is up to the editor.
 
So if you set the spacing of tabs to be the same as everyone else, does that mean you are using spaces or tabs?

And if you set tabs tabs to equal x number of spaces, does that mean you are are using tabs or spaces?
I don't see what's unclear. The question is about what's in the file: 0x09 or 0x20. If you set the tab spacing, you're using 0x09 and just changing how the editor interprets it.
 
I don't see what's unclear. The question is about what's in the file: 0x09 or 0x20. If you set the tab spacing, you're using 0x09 and just changing how the editor interprets it.

It just seemed that some people might be answering based solely on what key they are pressing.
 
It depends on the IDE or text editor you're using. In Visual Studio I always use tabs... because they end up being spaces. In vim I use spaces. In notepad I use spaces... and curse CRLF...

Same. Tabs in Visual Studio, in vim Spaces.
 
Spaces, with the tab key set to be 4 spaces in whatever editor i'm using at the time. Keeps thing consistent across almost any environment. Not as big of an issue as it used to be, and for some, maybe not an issue at all. When i was working with VB, maybe half the people i worked used spaces, but it wasn't a huge deal because the IDE dealt with it pretty well. At my current job (mostly java, some C), we occasionally open files (code, config, etc.) in vi on random remote machines, so we enforce a 4-space indent convention, no tabs allowed (the tab character that is).
 
Tabs. I like Tab = 4 spaces and I hate when looking at other people's code who use 2 spaces vs 1 tab as it looks crappy to me and thus I have to spend time replacing all of the indents with tabs before I get to work on it :p.

Its also much easier to indent / undent:)confused::D) by selecting an area and hitting tab or shift-tab to remove the indent.
 
Its also much easier to indent / undent:)confused::D) by selecting an area and hitting tab or shift-tab to remove the indent.
Are there programming editors that can't indent and outdent blocks over spaces?
 
Are there programming editors that can't indent and outdent blocks over spaces?

Dunno, but when I use tabs and someone else uses spaces it causes problems because my default tab is 4 while some people use 2, 3, or 4 spaces and thus stuff doesn't line up anymore.

If you like 3, I like 4 and bobby likes 2, why not use tabs and then everyone can have the code displayed how they like it? For alignment stuff spaces are fine, but for indenting I can't stand em.
 
Dunno, but when I use tabs and someone else uses spaces it causes problems because my default tab is 4 while some people use 2, 3, or 4 spaces and thus stuff doesn't line up anymore.

If you like 3, I like 4 and bobby likes 2, why not use tabs and then everyone can have the code displayed how they like it? For alignment stuff spaces are fine, but for indenting I can't stand em.

Would you not have the same problem using tabs because different editors use different widths for tabs?
 
Would you not have the same problem using tabs because different editors use different widths for tabs?

No, you wouldn't. If the \t character is in the file, the editor displays its version of \t. If 2, 3, or 4 spaces are in the file, it displays 2, 3, or 4 spaces. If everyone puts a tab in, everything lines up on their screen (probably, spacing beyond the first tab may be screwy if they use spaces there) If people put X spaces in, but X changes between people in one file, stuff doesn't line up anymore.

I still prefer spaces, but I work in console editors sometimes, so I like to restrict it to 80 chars, and I'm the only one working on my code (student) so yeah. Plus, it's the coding standard for my school.
 
Back
Top