views:

116

answers:

6

I know that this is more of a coding style, instead of a one right way of doing things. But, I'm a bit frustrated if I came across different indenting formats.

But, I would like to hear the reasons by various people on these issues:

  • Do you use spaces or tabs? Tabs with spaces? Any difference with "Tab insert space", instead of using the space key directly?
  • How many spaces to indent each line of code? Why?
  • Does different code has different style that is more suitable for each of them?

Is there a way to "visually" indent code without actually writing the indent? So it won't ruin the original indenting? It seems unlikely.

I'm using Xcode, so it's better if you have advice for Xcode projects.

A: 

A benefit of using spaces instead of tabs can make printing your code a bit nicer, especially if you're using a non-typewriter style font. I usually use a width of 3 spaces for each 'level' of indentation.

panic
Yes, a good print result is nice, but I think we can limit the standard of indentation for data on the disk or source code repository.
Jesse Armand
@panic @Jesse - "if you're using a non-typewriter style font" (ie. proportional as opposed to fixed width) then I would have thought it was the other way round!? You would need to use _tabs instead of spaces_ in this instance?
w3d
+1  A: 
  • spaces because they render the same everywhere
  • indent-width is context-dependent. runaway indentation is bad. generally, languages where you cannot easily reset the indentation to sane levels through named abstraction items (functions or methods) need narrow indentation.
  • yes, see previous bullet
just somebody
Yeah, I'm still inclined to using spaces.
Jesse Armand
+1  A: 

There are different programming languages with very different syntax. The syntax is crucial in choosing a coding style. Especially the tab size (number of spaces) that you will chose will depend on how many levels you need to have. In HTML for instance there are many levels, and there it makes no sense to have a big tab size, it even makes no sense in using tabs.. you just use spaces. Another very important aspect is the IDE you use. In some IDE's you are encouraged to use tabs (in Visual Studio you have the nice facility to add/remove tabs to more than one line (TAB to add, SHIFT+TAB to remove) and so on. Projects have different indent sizes because people use different indent settings (even in the same code) or because they use different editors or because the project includes code from other projects or even simply because people have different preferences or even different sized monitors. I work in Visual Studio C++ and VB.NET, indent is tab based(3 lines) and tabs do not insert spaces.

John Paul
Xcode is using spaces by default. I'm not sure what is the reason.
Jesse Armand
+1  A: 

Depending on your IDE and your "tab inserts spaces" option, it's a nice shortcut over using the spacebar. I.e. at 3 indentation levels with 3 spaces per tab, you press the Tab key 3 times instead of the spacebar 9 times. The net effect is the same - you inserted 9 spaces - but with 1/3 the number of keystrokes.

GalacticCowboy
For what it's worth, I normally indent HTML at 2 spaces and procedural code at 4. This is one area where tabs vs. spaces makes a difference - if someone else on the team preferred 3 and 3, the code would be formatted for them according to their preferred style while remaining formatted on my screen according to my own preference. However, I use spaces, not tabs... :)
GalacticCowboy
I'm still wondering whether tabs insert spaces will have different result compared to using spacebar directly. If it's the same, what's the point of using tabs with no spaces insertion?
Jesse Armand
@Jesse - Tab stops are at fixed positions across the screen, based on your tab settings. So when using tabs instead of spaces, indentation will be consistent regardless of your particular settings. If a particular line of code is indented at 3 tab stops, it will always be at 3 tab stops whether a tab is 2 spaces or 10.
GalacticCowboy
Yes, I just experimented with that. The issue with using tabs is (as John said) if someone else is not using tabs.
Jesse Armand
+1  A: 
  • I simply use the defaults of the editor I'm currently working in, but if I have the choice I use tabs because it means less invisible characters to manage. I have one editor that wholly manages indentation for me (REALbasic), another editor that manages indentation for me, allows for spaces as prefix but formatting gets wonky if I leave them in (Applescript), and there's Xcode.

  • I see 4 spaces more than another number for space-based indentation, so I go with that if I have to.

  • I've found that every community more or less has a standard that has been placed in example code, so I just go with that otherwise tabs then 4 spaces. Simple.

Honestly, I gave up on the indentation fight a long time ago and expend my brain power on other, what I feel are more important, code-related issues like good variable and functions names. Any reasonable code editor can help you manage poorly-placed or complex indentation but none of them can help with sorting out poorly-written or complex code.

Philip Regan
I understand. I also follow the project original indentation settings. But, it frustrates me when I'm working on multiple projects with different indentation settings.
Jesse Armand
+4  A: 

You should always use the coding style that the project is already using if you are modifying an existing project.

That being said, if you are able to choose your own coding style for a new project I suggest that you use tabs to indent code and not spaces -- here is why. By using spaces you force all of the other developers working on the project to conform to your indentation preference whether that be 2 spaces or 4 or 8 (or whatever). But by using tabs each developer can view the code using their own preference. You should uses spaces and not tabs to format text (to line up variable names one per line, or to line up multi-line comments) because that will work with any tab width preference. However, when you indent code use tabs not spaces. In short, indent with tabs because indenting with spaces is rude.

John Scipione
Correction, Xcode uses tabs inserts spaces by default. It seems they're going against your advice.
Jesse Armand
@John +1 on the use of tabs instead of spaces (I personally set my tab size to just 2 spaces). Also, why use 2, 4 or 8 characters when 1 will do? Only problem I've found with tabs is when posting code samples into forums etc. spaces often format better in this instance.
w3d
Jesse Armand: Xcode is wrong :) Goto Xcode->Preferences->Indentation Check the 'Tab key inserts tab instead of spaces' checkbox. Click the Ok button. http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/100-The_Text_Editor/text_editor.html#//apple_ref/doc/uid/TP40002679-SW64
John Scipione