Spaces or tabs in your code
-Chris
26 Replies
> Chapter 1: Indentation
Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.
Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.
Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning.
…
Read the rest of the very short document at
Cliff
@c1i77:
I was a 4-space softtab (my ~/.vimrc had sts=4) until I read the coding style guidelines from some guy named Linus Torvalds:
> Chapter 1: IndentationTabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.
Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.
Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning.
…
Read the rest of the very short document at
http://pantransit.reptiles.org/prog/CodingStyle.html Cliff
I have much respect for Linus Torvalds, but his guidelines are for use in the Linux kernel, and using C as the language.
Also, the editors I use all have indentation highlighting (dotted lines, or whatever) to enable very quick and easy distinction of code segments.
–deckert
And I use vim, BTW.
(Yes, this can cause problems with sendmail and syslog which use TAB as the seperator….)
I agree with Mr. Torvalds on this one, and I don't have the 80-column problem since I use a minimum of 120 columns in my terminals, often more. :)
Nothing is more aggravating then having to delete individual spaces to make things readable when a single stroke to remove that tab would be quicker.
That and tab is one keystroke
And no I'm not using those spawns of Satan called vi or emacs or any of their bastard children like vim
EXCEPT you sure better be careful on those hand full of ascii configuration files (makefile is probably the best example) that require tabs only for white spaces. I shutter to think how many total man hours have been lost through the years due to this one issue.
Hal Williams
I hate it when they get mixed. Ewwww…
I use two spaces for most things.
I use four for php - but I usually write php in a nice gui editor (bluefish) - in vi in a 80 character wide xterm, 2 spaces is plenty.
my ${HOME}/.vimrc uses a 4 spaces soft tab stob. which in my opinion works a lot better than a tab
Why? Because it's neater. It takes bloody ages to delete a whole load of spaces, whereas only a few seconds to delete around 3-4 tabs.
I code in SQL, and TABs are an absolute must to line things up. In PL/SQL code, however, i use a space for indentation so the code does not go off the side.
A kind of rule would be: space when it is block indentation, tab when it is clause indentation.
I use tabs at the front of lines to indicate indent level, while spaces are used after the code in a line starts to line up things.
1) I usually work with PHP, which doesn't care about tabs or spaces.
2) I always use a GUI editor, 1680 pixels wide.
But I think using tabs makes a lot more sense. That's what tabs are for, after all: aligning things. And it's nice to be able to define a tab size that suits you, regardless of what the others are using.
Those are too attached to tabs can use soft tabs anyway.
Even if you like using the tab key configure your editor to enter "4 spaces" for a tab. All good editors can do this. And if you use vim… this command is your friend:
:retab
@cmelbye:
I'm a Rubyist, so a size 2 soft tab for me.
+1