Pim
Pim

Reputation: 886

Are there practical or technical advantages to using 2-space vs 4-space indents?

Aside from taste, habit and personal preference, are there any advantages to using 2-space vs. 4-space indents while coding?

For instance, in this Google style guide, it is recommended to use 2-space indents for CSS.

Are there any technical advantages to using one over the other, for instance when transferring data between different systems?

Are there widely accepted conventions? (that possibly differ from language to language)

Upvotes: 29

Views: 45099

Answers (3)

Stephen C
Stephen C

Reputation: 719386

There are no clear "technical" advantages one way or the other. Indeed the only "technical" issue I can think of is the impact on the number of bytes in a source file.

  • If you represent the indents using space characters (ASCII SP), then 2 spaces is 2 characters fewer than 4 spaces.

  • If you allow TAB characters to be used, then (on Windows) a TAB indents by up to 4 spaces, so TAB characters result in fewer characters. But the flip side is that a TAB conventionally indents by up to 8 spaces on many other operating systems, so if you want your source code to look nice on all platforms you shouldn't use TAB for indentation.

And besides, it is common practice to "minify" CSS, Javascript and web-associated languages to make websites "faster". Among other things, that will strip out the indentation, rendering this minor technical difference to be moot.

(For the human readable version of a CSS, etc used by developers, the saving in transmission time / storage space is too small to worry about. Today's systems are optimized for the mass market, where storing and moving around gigabyte-sized files (movies) is common-place. Source code pales into insignificance.)


As to "practical" advantages, I guess it may be easier to view and edit files if you don't waste too much screen real-estate with deep indentation. From that perspective 2 character indentation is better than 4 or 8 character indentation. However this borders on being a "personal taste" issue ... unless:

  • you have to use a device that can only display (say) 80 columns, or
  • you have to work with code-style rules that restrict you to 80 columns.

Are there widely accepted conventions?

In general no.

In some languages, maybe, but I can't think of any. Even in Java, the most common convention for indentation is 4 spaces, but others are acceptable.

In software projects ... yes. It is common for a project style guide to specify this. For example, the Linux Kernel Style Guide quoted in Carlo's answer. (And even if this is not formalized, it "looks wrong" if a code base uses a variety of indentation styles, so a project's developers are likely to gravitate to a single style.)

Upvotes: 28

Carlo V. Dango
Carlo V. Dango

Reputation: 13902

From: Linux Kernel Coding Style

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program. In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning

Upvotes: 42

user207421
user207421

Reputation: 311023

I used two spaces in my books because otherwise you run out of horizontal space pretty quickly. I use four in the IDE.

It used to be a physical tab, eight spaces, in the old days of course, but code got too nested for that many decades ago.

Upvotes: 22

Related Questions