Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 705
Pim Avatar asked Oct 30 '14 22:10

Pim


2 Answers

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

like image 187
Carlo V. Dango Avatar answered Oct 12 '22 16:10

Carlo V. Dango


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, the saving in transmission time / storage space is too trivial 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.

like image 14
Stephen C Avatar answered Oct 12 '22 17:10

Stephen C