开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《免费学习社区 freeCodeCamp》
今日推荐英文原文:《Tabs vs. Spaces》

今日推荐开源项目:《免费学习社区 freeCodeCamp》传送门:GitHub链接
推荐理由:freecodecamp.org是一个免费学习编程的社区网站,课程完全免费并且自主安排。网站有数以千计的交互式编码练习,以帮助您扩展您的技能。
今日推荐英文原文:《Tabs vs. Spaces》作者:Kenneth Reilly
原文链接:https://medium.com/@kennethreilly/tabs-vs-spaces-3c24defa7c9e
推荐理由:关于是使用Tab键还是使用空格键缩进的争论在编程界已经持续了多年,不过Tabs可能是更好的选择,是因为空格需要多按几下吗?

Tabs vs. Spaces

A scientific approach to an endless debate

Introduction

The debate over whether to use a tab or a space for 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 Indentation

We 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.

The spacebar key, 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 tab versus the space when it comes to typing content in a readable fashion, the tab wins 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 tab exists for the sole purpose of indenting content while the space does not, let’s examine the efficiency of using each.

Efficiency

There 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.

Conclusion

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. 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).


下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/