Code and Coffee


Tab or Spaces?

Posted on August 22, 2006, under Development.

Code syntax has long been a debate amongst developers. Tabs or spaces, bracket on newline or same line, comment colors, Hungarian notation, and the list goes on and on.

Personally I use tabs, and usually set my IDE settings to eight spaces wide. I do not know why, it’s just what I have always done and it allows me to read code better. I am also a bracket on the newline kind of guy, I find it works better with code folding features that some IDE’s have. As far as Hungarian notation, it’s a must for me. I can’t stand one or two letter variable names; it reminds me a lot of spaghetti code. One of the great features that some IDE’s have is code reformatting, where with the click of a button you can reformat code to your liking. However, this sometimes throws off source code control and angers other developers. So I usually do it in a temporary file if I absolutely can’t read a thing.

What are your code syntax pet peeves? What is your coding style?

Popularity: 9% [?]



8 Replies to "Tab or Spaces?"

gravatar

cinac  on August 22, 2006

I saw an article online someplace that used the best of both worlds. Use the tab key to indent your code, and then use spaces for alignment as needed (for parameter lists, etc. that you might want to split onto separate lines).

I have since switched to doing this, and it’s quite nice. No matter what tab size someone’s editor is set to, aligned items stay aligned and the indents can float in or out as needed.

gravatar

Doug  on August 22, 2006

Ugh tabs in code. This is a big problem when working with many developers on the same code because some people use spaces and some people use tabs and it all comes out looking like crap.
Most modern IDEs allow you to automaticly convert tabs to spaces, so when you hit a tab it comes out as 2 (or 4) spaces; there should be no excuse to use tabs.
One of my major pet pevees is when people are inconsistant within their own code about how they format things. Use whatever notation you want, just be consistant. OK, anything except Hungarian notation (shudder).

To be fair I can see the value in it when used Apps style:
http://www.joelonsoftware.com/articles/Wrong.html

I feel that if my code is complicated enough that I need a variable naming scheme I must have done something wrong. Besides this is what IDEs are for.

gravatar

mattrmiller  on August 22, 2006

I use tabs because when you use spaces it sometime becomes annoying, like when you use the arrow keys to go left or right with spaces you have to hit it several times. There are other reasons that I can’t think of at the moment, but that’s the first that comes to mind.

gravatar

RMF  on August 23, 2006

Isn’t the point of tabs that they can be adjusted to a displayed length at the discretion of whoever is currently coding? For example the author here mentions that he sets his tabs to 8 spaces. 8 Spaces makes me shudder, so I set mine to 2. We can both edit the same come and see it how we like without modifying all the whitespace and potentially causing merge nightmares once it goes back into revision control (yeah I know merges should ignore whitespace changes, but this isn’t always true).

As for spaces, I don’t see the advantage. Sure you can space, space, space, over and vertically align function parameters. which looks pretty, but gets blown out of the water if you refactor the function name.

And as for brackets and variable name: Brackets on the same line as the “if”, “else”, etc. or you’re fired (:P) and verbose variable names are best.

gravatar

mattrmiller  on August 23, 2006

That’s a another good point about tabs. What is your reason for brackets on the same line? Just curious.

gravatar

RMF  on August 23, 2006

I really have no pragmatic reason for the brackets style, I just have always done it that way and prefer it. Probably because it uses up less vertical space, and I see no reason to use more than 1 line for an “else” statement:


if {
this;
} else {
that;
}

just seems more concise to me. Either way though I thinks its most important to decide on a code style for your team and stick with it - newlines before brackets or not, consistency is key when multiple people are editing code.

gravatar

RMF  on August 23, 2006

Of course HTML renderers ignore my beautiful whitespace so after evangelizing tabs I post a non-indented example… noooooooooooo

gravatar

mattrmiller  on August 23, 2006

I usually strip extra whitespace from my HTML, Savant the PHP template engine of my choice has a great feature to do this. I find it helps with page loading a little, as it’s less to download.