Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabs vs Space indentation [closed]

Tags:

coding-style

If you read any coding style guide, there is a big chance you will see a recommendation on using spaces instead of tabs for indentation. Some guides explicitly say: never use tabs.

I know that there is a risk of starting a nuclear war about tabs vs spaces. It is surely not what I intend. What I do intend, however, is to ask you if there is any good reason for this recommendation. Why does everybody keep saying that spaces are the best way to indent code?

like image 297
Ionuț Staicu Avatar asked Jul 15 '12 13:07

Ionuț Staicu


People also ask

Should I use tabs or spaces for indentation?

So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.

Why do some people use spaces instead of tabs?

If a couple of people work on same file it is highly possible to generate unnecessary conflicts. Using spaces instead of tabs makes it possible to easily catch such an accidental space on eyeball and this is probably a reason, why using them become a standard.

Is tab the same as 4 spaces?

Answer. In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.

Do tabs take up less storage than spaces?

Yes. A tab is one character in size. Each space is also one character, so if you indent using 4 spaces it takes 4 characters, and 8 spaces would consume 8 characters. This is absolute common sense; why do you need to even ask?


2 Answers

Getting tabs to look right depends heavily on the configuration/choices of all the software the source code might be displayed or printed by. If you have a restricted set of such software and find that's not a practical problem for you, that's great - go for it if you see value. But, be aware that when a line is indented with tabs, then continued on subsequent lines where you want indentation based on the position of an opening parenthesis, to line up quotes etc. - you'll be mixing tabs and spaces in a way that's visually impossible to verify the correctness of (unless your editor shows tabs differently). Countering that, using arrow keys to cross tabs, or delete, can be faster but also more confusing and frustrating. It's rare for different people using different tab widths to work cleanly on the same code, which is perhaps the main promise of tabs. Personally, I use spaces.

like image 71
Tony Delroy Avatar answered Sep 29 '22 13:09

Tony Delroy


Tabs can be a different size on different computers and printers. The code might look great on the original computer, but on another computer or printout the spacing might look all weird.

With spaces, the code will look the same regardless of the computer.

like image 30
Starkey Avatar answered Sep 29 '22 14:09

Starkey