Regarding the debate between Tabs and Spaces, the hot-tempered brother Linus has new instructions!

2024-05-14 10:42:10

The debate between Tabs and Spaces is an ongoing controversy that dates back to the Unix era. Linus Torvalds has long been known for his candid feedback, and despite efforts to restrain himself since 2018, recent events suggest the outspoken veteran is back, albeit with more civilized and polite wording. In the newly released Linux

The debate between Tabs and Spaces is an ongoing controversy that dates back to the Unix era. Linus Torvalds has long been known for his candid feedback, and despite efforts to restrain himself since 2018, recent events suggest the outspoken veteran is back, albeit with more civilized and polite wording.

In the newly released Linux kernel candidate version 6.9-rc4, in addition to the routine updates to drivers and bug fixes, there are also ongoing adjustments and optimizations to the bcachefs filesystem, as well as mitigation measures for the newly detected Spectre-style local branch history injection vulnerabilities, aiming to maintain system stability and security. However, a notable change is Linus’s tweaking of configuration file variables himself, where he chose to make a subtle change to “Kconfig” by introducing a hidden Tab, thus replacing the original space indentation. The purpose of this is to phase out poor-quality parsing tools in order to improve performance and stability.

Taking the kernel source code as an example, we can see that the space between the “default” value and the subsequent integer value is now replaced by an eight-character width Tab indentation. Since these indents fall at the end of a Tab stop, they appear as a single space character, which could lead to parsing errors. Kconfig is a configuration language used to manage the kernel build process, utilizing indentation to differentiate between different blocks of code, similar to many other indentation-reliant languages.

In one of Linus’s code commits (commit d96c36004e31), he discovered a particular code change, the main purpose of which was merely to fix an item, FTRACE_RECORD_RECURSION_SIZE, by replacing Tabs with spaces, to ensure the Kconfig parser could read the file correctly. In this change, Linus deliberately made the indentation mechanism in the kernel build configuration files more complex, challenging and motivating other tool developers to improve the processing capabilities of their parsers. Linus showed a proactive and deliberate attitude by adding hidden Tabs to catalyze developer improvement for related tools, clearly reflecting his commitment to code quality and tool refinement.

In the modern field of software programming, code readability is of utmost importance. Hence, code indentation is fundamental to building a coherent and readable code structure. The use of indentation helps developers establish a visual hierarchy of code, facilitating the organization and distinction of conditional statements, loops, methods, and classes. It not only clearly shows the scope of code lines but also significantly enhances code readability and logical structure.

The importance of indentation in programming goes beyond just delineating code hierarchy. It also helps break down complex logical operations. For example, nested conditional ternary operators can be split across multiple lines with proper indentation, clarifying the relationships between different expressions. Long chains of method calls are often indented on new lines to highlight that they belong to the same series of operations. Indentation is a tool designed for human developers with the purpose of simplifying the reading and coding process, making it more intuitive and efficient.

Among programmers, the debate over using the Tab key or the Space key for indentation has always been contentious and has a long history in the tech community. In some contexts, this argument can even escalate into sources of light-hearted humor or emotional entanglements—as shown in the plot of the HBO series “Silicon Valley,” where two programmers in a relationship part ways over their preferred methods of indentation.

Adherence of the Linux Kernel and Coding Style:
In response to this long-standing debate, Linus Torvalds, the maintainer of the Linux Kernel project, has taken a firm stance. He advocates for the coding principles promoted by Brian Kernighan and Dennis Ritchie, the authors of “The C Programming Language”, aiming to keep code style consistent and efficient. In the Linux Kernel, the standard practice is to use Tab for indentation, which typically signifies eight characters—this choice is historically tied to teleprinters and line printers.

Linus recently used more complex indentation intentionally to force inefficient parsing tools to improve or be phased out. This reflects his stringent standards for software tool quality and his commitment to coding discipline. His opinion appeared in the fourth RC (Release Candidate) of Linux Kernel 6.9 and noted that no significant issues occurred that week. Differing from his previous intense statements, this time he expressed his views in a more subtle manner without using harsh language.

Notable Individual Views on Indentation:
Microsoft founder Bill Gates expressed his view on the Tab vs. Space debate in a Reddit interview. Gates prefers Tabs, arguing that they align columns better, making the code easier to edit and review. According to him, when dealing with certain Word documents, Tabs not only improve operational efficiency but also optimize the document’s formatting aesthetics.

In the programming domain, the discussion on using Tab characters or Space characters remains controversial. For most programming languages (except space-sensitive languages like Python), compilers usually aren’t affected by the code’s formatting style. The key dispute over Tabs and Spaces isn’t about the exact number of characters appropriate for indentation, but which method should be chosen for indentation.

Tab characters: Quite simply, one keystroke generates a Tab. Compilers interpret Tabs as a set number of spaces, often 4 or 8. Tabs generally make document files smaller, as they reduce the number of characters needed, and many modern editors and Integrated Development Environments (IDEs) handle Tabs easily. Since Tabs can be interpreted as different lengths in different IDEs and editors, there is a high degree of configurability—they can be set to represent 6 spaces, or any other number. This flexibility makes Tabs a favorite for developers who like visual tidiness, believing they should be used for indentation, while spaces should be used for alignment.

Space Character: A core concept adhered to by proponents of the space character is consistency. Since different platforms and editors interpret Tab characters with varying amounts of indentation, the portability and permanence of code might become problematic. If code is copied and pasted from elsewhere, differing indentation levels could lead to disorganized code formatting. Meanwhile, spaces maintain consistency across all editors and platforms, ensuring that the display format of the code remains the same in different environments. This means that the author of the code has solidified the formatting choice within the code, and other readers are unable to adjust it and must passively accept it.

The heart of this debate touches on core issues that extend beyond the technical level, involving different programming philosophies. Enthusiasts of Tab characters believe that the display structure of the code should be decided by the reader. On the other hand, proponents of spaces argue that ensuring consistency is crucial, and this must be guaranteed by the code authors. Both sides have ample arguments supporting their positions. Nevertheless, when faced with these two distinct choices, the ultimate decision still lies in the hands of the developers.

On this matter, Google programmer Felipe Hoffa once revealed an interesting phenomenon through analyzing a large number of code repositories on GitHub: overall, the use of the space character is far more common than the Tab key. So, will you be on the side of Tabs, or the side of spaces?