Widget Image
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna

Sorry, no posts matched your criteria.

Tabs are better than spaces

When it comes to formatting code, developers can be very particular. Camel case vs snake case vs kebab caseoptimal line wrapping length, and single line vs multiline function calls are among the more common arguments. Most of the time I have a preference, but no strong argument either way. However, when it comes to tabs vs spaces, there is a clearly better choice: tabs.

The tab key was originally invented on typewriters to make it easier to format information in “tabular” form. It was intended to minimize the amount of spacing and backspacing necessary, and to make formatting easy and consistent on the page. The tab width was set by using adjustable clips on a bar across the back of the typewriter so each typist could set their own preferences.

Fast forward to today with computer keyboards, and we still have the tab key. Its most common use remains indentation.

Spaces keep it consistent

In the not-too-distant past, most programs that displayed text would choose their own tab width and that was it. No configuration, no choice. This would make code indentations vary widely across applications, with some programs using a tab width of 8, and others 4, or 2, leading to readability issues. To combat this, some developers switched to using spaces instead of tabs, and had their IDEs automatically insert a set number of spaces when the tab key was pressed. It ensured that the indentation would be consistent across all environments, regardless of tab width.

So what’s the fuss?

Different people like different tab widths; it’s a preference as personal as any other. But more importantly some people need different tab widths. Developers who are visually impaired may need either smaller or larger tab widths to be able to read the code, depending on their particular situation. Additionally, there are situations in which varying tab widths can be quite useful. For example, a smaller tab width can be helpful viewing code snippets on a mobile screen, whereas larger tab widths may be preferred viewing lots of indented code on a wide monitor. The point is, consistency across environments is not actually a benefit, readability across environments is.

Tabs are the solution

An adjustable tab width is the perfect solution. In fact, even back in 1900 when tabs on typewriters were patented, they were adjustable. Surely 120 years and a technological revolution later, we shouldn’t be abandoning tabs because of tooling configuration limitations. Using tabs allows everyone to get their preferred width, includes visually impaired developers, and also allows dynamic changes across varying environments. And as a very small bonus, they make for slightly smaller file sizes than spaces. These considerations taken together make tabs the clear winner in the tabs vs. spaces debate.

I'd love to know what you think

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from JavaScript Artisan

Subscribe now to keep reading and get access to the full archive.

Continue reading