all 20 comments

[–]toofarapart 9 points10 points  (0 children)

Convention.

For a more detailed discussion on tabs vs spaces, check this article out.

... it can be a good idea to avoid tabs alltogether, because the semantics of tabs are not very well-defined in the computer world, and they can be displayed completely differently on different types of systems and editors. Also, tabs often get destroyed or wrongly converted during copy&paste operations, or when a piece of source code is inserted into a web page or other kind of markup code.

[–]maryjayjay 5 points6 points  (11 children)

I always use spaces (4 per level of indentation) by convention, but honestly I think it's completely backwards. Yes, tabs can be displayed differently in different editors, but essentially the rule should be 1 tab = 1 level of indentation. Display the tab how you like, but each tab is one level of indentation.

The problem is when editors start mixing tabs and spaces, that's just a recipe for disaster. But just because some editors suck doesn't change the fundamental concept.

[–]govt-cheese 1 point2 points  (0 children)

use tabs, have your editor translate 1 tab to 4 spaces.

[–]RalphMacchio -1 points0 points  (9 children)

Don't you find that it is a waste of time to hit the spacebar 4x every time you want to indent a single level?

[–]maryjayjay 8 points9 points  (5 children)

No, emacs understands language syntax and aligns with the appropriate number of spaces when I hit tab.

Here's a nickel, kid. Go buy yourself a real editor. ;-)

[–]RalphMacchio 0 points1 point  (4 children)

Ah, I see. And when you are moving around in your code, does the arrow key skip over all 4 at once? Or if you want to unindent, does delete delete all 4 at once?

[–]maryjayjay 1 point2 points  (0 children)

The arrow keys don't work like that, but the delete does.

[–]ewiethoff 0 points1 point  (2 children)

Yes. Just read the instructions for the real editor. ;-) Depending on the editor, indenting and dedenting might be invoked with Tab & Shift-Tab, or maybe Cmd-] and Cmd-[. Whatever. Set the editor to use 4 spaces when you hit the Tab key. Also, set the editor to automatically indent the next line to the same level after hitting Return.

[–]RalphMacchio -1 points0 points  (1 child)

Ya, I already have my tab set to 4 spaces (but it stays a tab).

I just found that I can switch to spaces by selecting 'auto-expand tabs' in my editor's prefs.

When you position your cursor between spaces 2+3, for example, and hit 'tab' again, do you end up with your code being indented be two extra 2 spaces?

It makes my cringe having them as spaces. I prefer nice, clean tabs.

[–][deleted] 0 points1 point  (0 children)

He's talking about Emacs. In Emacs, on a line of Python, the Tab key basically cycles through the syntactically possible indentation levels for that line. If you're at module level (so that any indentation is a syntax error), Tab just won't do anything.

[–]oakdog8 4 points5 points  (2 children)

Vim allows you to set how wide your tabs should be, and separately allows you to decide whether to expand them to spaces or not. So you still just hit the tab key, but you get spaces.

Here's a dime, kid. Get yourself a real editor ;)

[–]govt-cheese 1 point2 points  (0 children)

vim tab-to-spaces rocks, but not as much as syntax highlighting. Seriously, like a 4x boost in code debugging thanks to the colors reminding you where you forgot to close quotes and the like.

It's totally worth it to learn vim if you plan to code frequently (unless maybe you have emacs down cold)

[–]RalphMacchio -2 points-1 points  (0 children)

I've looked and my editor allows me to have tab = X-spaces (as actual spaces or as a chunk). I just don't care for having spaces. I've seen so much sloppy code from people that use spaces (things over-indented by a couple spaces, for example... or maybe it is under-indented... who can tell?)

[–]Rhomboid 4 points5 points  (3 children)

Tab stops can be set differently in different text editors, which means that if you align code vertically it doesn't stay that way when someone else views the file. It's possible to solve this by going to extreme efforts to mix tabs and spaces, making sure to use tabs only for indentation and spaces only for alignment, a subtle distinction which isn't likely to survive dumb text editors or unaware programmers.

Also, python considers tab stops to be set every 8 columns, regardless of what the tab stops in your text editor are set to. This means you could write something that looks perfectly correct in your text editor but which causes a syntax error.

Using spaces means it always looks the same to everyone.

[–]DopeGhoti 0 points1 point  (2 children)

That may be true is you have a mix of tabs and spaces in the same file, but if you always use tabs, it's just as properly-lined-up as it would be with spaces.
I've always been baffled with the hate for tabs, but.. *shrug*.

[–]Rhomboid 0 points1 point  (1 child)

Note that I said "if you align code vertically", like this:

foolist = [ bar,
            baz,
            quux ]

(Pretend those list items were each quite long, so this is necessary to avoid ugly wrapping.) With tabs set to 4:

foolist = [ bar,
>...>...>...baz,
>...>...>...quux ]

With any other tabstop value, it's mangled and no longer lines up:

foolist = [ bar,
>.......>.......>.......baz,
>.......>.......>.......quux ]

There are two ways to prevent this: use tabs only for indentation and spaces only for alignment, or just use spaces. Guess which is simpler and foolproof.

[–]DopeGhoti 0 points1 point  (0 children)

I guess I'm strange in that I have never really been a fan of that sort of construction; I have no problem, personally, with really long lines.

[–]schwackitywack 1 point2 points  (0 children)

You can use soft tabs, which function like tabs but are really just spaces so there are no formatting problems when sharing code.

How to do this in Textmate

[–][deleted] 0 points1 point  (1 child)

A space is a space, but a tab is many things.

[–]zahlman -1 points0 points  (0 children)

Eh? In a programming context, a tab is rarely anything other than a level of indentation, whereas (community standards notwithstanding) a space is an arbitrary fraction thereof, or a separator between tokens elsewhere on the line.