今日推荐英文原文：《Tabs vs. Spaces》
今日推荐英文原文：《Tabs vs. Spaces》作者：Kenneth Reilly
Tabs vs. Spaces
A scientific approach to an endless debate
IntroductionThe debate over whether to use a
spacefor indention has been long-running for many years in the programming community.
I use tabs for indentation myself, originally because that was what our senior developers used at my first software job, and to this very day for various reasons, most of which are logical and mathematical in nature. In this article, I will outline these reasons and shed light on this topic.
Code IndentationWe indent source code to make it more readable for humans, and for no other reason. This is fundamentally no different than indenting a list on a page in an old-school textbook or other document, for which the tab key was developed and included in every major typewriter design in history.
spacebarkey, on the other hand, was not developed for the purpose of indenting content on a page, but for separating words in a sentence. So, when we inspect the
spacewhen it comes to typing content in a readable fashion, the
tabwins by virtue of having been designed for the specific purpose of indenting text in order to make it more readable.
So now that we have examined the true purpose behind each character and reached the conclusion that the
tabexists for the sole purpose of indenting content while the
spacedoes not, let’s examine the efficiency of using each.
EfficiencyThere is an unspoken concept among many senior developers who grew up in the 80’s and 90’s with limited tech resources, that many programmers today have a bad habit of taking for granted the kind of power and bandwidth that are available to the modern software developer. While it’s a good thing that we have the kind of resources we have today on modern client, server, and networking machines, it’s still a bad habit to assume that raw power will make up for a lack of efficiency on the part of the developer.
While the machine doesn’t actually care if you use tabs or spaces (unless you’re using Python or something similar in which whitespace is part of the syntax), the size of your source code will increase by the use of spaces over tabs, especially if you indent by four spaces. If you are five levels deep in a nested function and you’re indenting an expression like
String str = ''with 20 spaces, that line of source code contains more indentation characters than it does code, which is truly absurd by any standard. Multiply that by hundreds (or thousands) of files, with hundreds of lines of code each, and you’re going to have MBs upon MBs of spaces everywhere.
ConclusionSo, 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. It’s also arguable that full tab indentation produces more readable code, and will encourage a developer to utilize proper abstraction, encapsulation, and other practices, since not doing so will result in highly nested logic with indentation going off the screen (something that isn’t as much of an issue when indenting with two spaces).
How you see your code every day has a huge impact on the kind of success you will have long-term as a developer, and clean logic is much easier to read and requires fewer mental resources (meaning more can be put into the actual work you’re doing instead of in maintaining hard-to-read code).