views:

5424

answers:

22

I sometimes find myself discussing this issue with other C# developers and especially if we use different styles. I can see the advantage of tab indentation allowing different developers to browse the code with their favorite indent size. Nonetheless, I long ago went for two space indentation in my C# code and have stuck with it ever since. Mainly because I often disliked the way statements spanning multiple lines are sometimes messed up when viewing code from other developers using another tab size.

Recently a developer at one of my clients approached me and asked why I did not use tabs because he preferred to view code with an indentation size of 4.

So my question is: Which style do you prefer and why?

+33  A: 

TAB because it is easy to get the indentation size you like(2) and the ones that other people reading your code like. The inverse transformation, from double space to TAB, is difficult.

Ovidiu Pacurar
+1 Just please never use tabs beyond the structural indentation (i.e. the far left).
Mattias Andersson
-1 Never seen a team capable of not making mistakes with this scheme.
romkyns
+1  A: 

I prefer my style, that's why I use Ctrl-A Ctr-K,S (Format doc) to format the file according to my rules, if I don't like the way it's indented.

At my shop we had this argument too, and we finally gave it a rest, people prefer different styles, so they should use their preferred style, because it makes the working environment more comfortable, and also makes them more happy ...

Pop Catalin
This is not a good idea if you are checking code into a version control system. It will make it impossible to do a diff on your changes.
Lars Fastrup
then you must use the diff`s Ignore White space option. If the diff cannot do it then change your diff tool.Of course it would be a bit difficult to do with python, then again the question is C# specific
Newtopian
@Pop: You know that in Visual Studio Ctrl+K,D does the same thing with less keys.
Jeff Yates
@ffpft, I always forget the key combination to format the entire doc, so I always use the format region one :P (too much keys)
Pop Catalin
@laf we never had any serious diff issues (or any issues) because of this. Also after a time everyone seems to adopt the team's best style anyway :), but at least you don't put pressure at first, the change is gradual.
Pop Catalin
+22  A: 

Personal Preference: C# tab = 2 spaces, C++ tab = 3 spaces, and let Visual Studio convert them to spaces.

For me the core of the "tab vs. space" discussion is not indentation, but in-code alignment.

Tabs can provide "everyone's favorite indentation". However, if you like to line up statements like this:

switch(foo)
{
   case bar:      Do1(); break;
   case foobar_2: Do2(); break;
}

then, Tabs are evil. Rather, non-lead tabs are evil.

So a coding standard that requires "initial line indentation must be tabs, no other tabs are allowed" would technically be the best two worlds. However, I would find such a coding standard to much obsessed with a petty detail. There are more important things.


Anyway, a shop should be able to pick one of the standards, and developers should be able to stick to it, whichever it is.

peterchen
I doubt I would ever force other people to adhere to this, but this is the way I do things.
vfilby
That style would get you castrated in our shop!
John Kraft
John, It's the style I prefer, and happily I am in the position to dictate it. But the point of my reply is: Accept what the shop dictates - as long as it's not counterproductive.
peterchen
I think it's the style preferred by people who got weaned in programming in the 80s. It only really mattered in COBOL.
Jon Limjap
I never did cobol, but I have no problem showing my age :)
peterchen
This style also requires a fixed-width font. That's a pretty far-fetched requirement for an "indentation style" IMHO.
bzlm
I've tried a proportional font once, shuddered, and never looked back. Might depend on the language, though.
peterchen
(warning - assumption) Ideally, the Do1/2 methods and break keyword should be on a separate line, indented properly under their appropriate case statement. I think your argument for "best of both" falls apart in this case.
Goyuix
Why 2 in C# and 3 in C++?
Svish
Personally I'd prefer 2, 3 was a compromise at work between 2 and 4. Silly, I know, but now I got used to it. There's usually more "base indentation" in C#, so I started using 2, no prob since work is still mostly C++.
peterchen
At least 3 works as a compromise because no side "wins" and everyone is in the same boat---may sound silly, but it's good for morale on an issue that really, truly isn't significant. That said, have you looked at elastic tabstops?
Roger Pate
@Roger Pate - Elastic Tabstops: Yeah, I wish Visual Studio would support them. But with the state it is in, they'd probably break C++ templates on the way.
peterchen
`"initial line indentation must be tabs, no other tabs are allowed" would technically be the best two worlds` I agree, but of course, this is an impossible coding standard for even a small group of people, so this would **have** to be done automatically by the IDE. As far as I know, no IDEs do this currently.
BlueRaja - Danny Pflughoeft
+15  A: 

I prefer elastic tabstops. Unfortunately, software support for them is very limited.

bzlm
thanks for reminding me of the "official" term :) I'd love to try them out in my day-to-day environment.
peterchen
Same here. They could breathe new life into this tired old discussion.
bzlm
+1 this is an amazing idea
BlueRaja - Danny Pflughoeft
+8  A: 

Me and this guy on my team, we're like dogs marking territory by urinating on it. I mark my territory by using tabs. When he happens to change some code in my territory, he marks it by using spaces.

It's kinda a fun relationship, like one of those movies with mismatched cop buddies...

Corey Trager
funny, sad, but true. +1
kenny
Ctrl+K+D and you can dominate everything!
Jeff Yates
Why not urinate directly on that guy instead? Don't bring your code into your power struggle!
bzlm
@bzlm - Good idea!
Corey Trager
You can extremely quickly see if your code was changed :)
ya23
Your source versioning history must be fun
Richard Szalay
+3  A: 

I'm a white-space bigot, sorry. I think of code as prose, so we should follow the rules of good writing as much as we can. I would never write a sentence with examples(like, this), so should we write code that way? Also for me, tabs are a sin. They cause problems as you integrate/use external tools. IMO, always set the environment to replace tabs with some amount of spaces, but for me the number doesn't matter as long as the team settles on it.

kenny
+3  A: 

I prefer to focus on writing good, solid code. Format it so it's generally readable under a number of circumstances. I used to get grumpy about dealing with "whitespace incompatibilities," but frankly life is too short, there is too much code to be written and too much to share to be stuck on this. If you've solved all other problems and are shipping on time, then I think it's fair to focus on whitespace and the One True Brace Style.

plinth
Yes, life is too short. Too short for false diffs in source-code check-ins/merges because of different tabbing...
Tor Haugen
If you have false diffs from different tabbing, your diff tool sucks.
plinth
+3  A: 

I don't see much of a problem when the enviroment is homogeneous enough to the code show properly in every editor used. But when the source is shared among users of several editors, or when the users customize the way tabs are showed AND some use spaces, some problems start to happen. In the past we let the developers use tabs or spaces, whatever they wanted. Someone used tabs to show 2 spaces in their editor. Another developer customizad eclipse to show 4 spaces. And most used 2 spaces, with the editor showing the default 3 spaces for tab.

Then people started to edit the source, and not everytime formated the entire code. When the tabs as 2 spaces station shown the code, everything was fine. But if anybody else opened the source for editing, there was several misalignment.

For a while only forcing people to reformat the code at saving was enough. Until some clients had some strange rules that forced us to customize some code inside their house. Not even a notebook on their network was permited, the enviroment was theirs. And the tabbed code was pratically unreadable in some stations, where a tab could expand to 8-10 spaces on some 40 column console...

This forced us to reformat all the code to use 2 space ident. It was a severe pain, but became unavoidable... so the rule is: if the code can be read/edited by some arbitrary developer, or in some arbitrary enviroment, it is a good practice to ident using spaces.

Renato Soffiatto
+9  A: 

Using tabs is the only way to go. It's freedom baby. Spaces are for fascists!

Tabs let anyone viewing it choose their own preferred indent size.

Tabs also make the size of the code files smaller. I know this isn't a big deal, but it is something. When you're using SVN over a slow connection, it all adds up.

GeekyMonkey
+2  A: 

Given Visual Studio's powerful formatting tools, I don't think it's necessary to choose anymore, as long as everyone is using the same rules, more or less.

Just delete and replace the last brace in a file and VS will reformat according to the rules defined in your settings. If I am making substantial changes to a class, I will reformat the whole file. Otherwise I leave it alone. No point checking in just white space changes.

It just doesn't matter anymore.

Ishmael
How I wish more people would just get this. It can be tricky to agree on a format, but if you manage (like we did in our team) then what follows is pure formatting bliss. Visual Studio just does it for you. Never need to give it a second thought.
romkyns
+3  A: 

Tabs are inherintly evil.

pearcewg
A: 

get some practice with python. Spaces is the only way to go.

Kozyarchuk
Only if they are inserted by your editor when you press Tab :)
TM
Python fanatics have an inherent problem with believing that they're format is the right format.There is no evidence to support this, of course.
Dalin Seivewright
@TM: Of course your editor should insert spaces when you use Tab! It it should be able "untab" spaces too! Sometimes I get the idea Tab proponents think space proponents press 4 x spacebar everytime :p
catchmeifyoutry
+12  A: 

Tab has a semantic meaning. So go for tabs. That is why they exist in the first place.

baretta
A: 

I prefer a hard tab of 5 with virtual whitespace turned on.

Dalin Seivewright
+1  A: 

Tabs. I prefer 4 spaces and my enemy in the next cubicle prefers 3 spaces, and both get what we want.

And please don't mix them: one tab and then two spaces (especially when it comes to intending method/function arguments based on the name) is evil and breaks the beauty of the code.

Nivas
+3  A: 
+1  A: 

Obviously tabs are better.

Obviously spaces are better.

The correct answer is:

It doesn't matter, as long as you are consistent.

Orion Edwards
+1  A: 

This is kind of a pointless argument - in Visual Studio Ctrl+K+D reformats the file to use your preference anyway.

Make sure they're the same preferences as everyone else on your team or they'll hate you when they get a merge.

Keith
Yes, yes, yes! .
romkyns
So you're saying I can use my preferences so long as they are also everyone elses preferences?
Tor Haugen
Basically yeah - either everyone uses tabs or everyone uses spaces. Otherwise merges get ugly. You have to agree with everyone and use the same settings. Once you have, for any user, if they hit Ctrl+K,D it will fix all the formatting to be your standardised style.
Keith
A: 

Tabs are sometimes displayed differently in different editors. So when using just one editor, it doesn't really matter. But if you jump between different ones, it can start to be a problem if you have used tabs.

As an example, I coded a bunch of html/css/php files in Dreamweaver (using tabs) and they all looked pretty. Then one day I needed to fix something in some of the files through ssh, where I only had access to vim. They didn't look as good there... to put it like that... Of course I'm sure you can make vim work the same way as Dreamweaver, but using spaces is just... simpler... =)

Also, to those who mentioned it in other answers/comments here: Are there seriously people out there coding with non-fixed width fonts?? I would go insane...

Svish
+2  A: 

I am a spaces guy - but that said the most important thing is to be consistent in a team. It will otherwise be very frustrating to share and review code among the members. File compare will also be a problem if developers constantly reformat code to their preferred format.

laf
+1  A: 

I prefer using tabs with 4 spaces.

With 2 spaces, blocks look compacted to the left and it always leaves me with the feeling that the logical structure of the code is being "watered down", in terms of readability. The choice between 3 and 4 spaces is aesthetic. Going above 4 doesn't really produce any kind of gain for me; However, eventually the spacing becomes too large and this tends to be distracting.

The reason for my preference of using tabs is convenience when typing.

Derek E
+1  A: 

Tabs.

Spaces are annoying when navigating.

Anyway, all I ask is a choice. And I visual C# 2010 I cannot find such a setting.

What also annoys me is that it insists on formatting like this:

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "Hello, World!";
}

When I want to format it like this:

private void button1_Click(object sender, EventArgs e) {
    label1.Text = "Hello, World!";
}

Your opinion on my formatting is irrelevant. I should have the choice.

Pete