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?
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.
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.
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.
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With