PSR-2, analysis of one paragraph of the standard. Spaces or tabs

Here I decided to completely switch to the style of code design according to the PSR-2 standard. But being the enemy to retreat spaces (I believe that SmartTabs is the best option), I decided to explore the question: What is written in the standard, and why this is so. Chased.

What the standard says


Let's look into the text of the standard and see what is written there:
1. Overview
Code MUST use 4 spaces for indenting, not tabs.

In the quick reference they write that the code SHOULD use 4 spaces, but not tabs. Okay, let's read further about this item in more detail.
2.4. Indenting
Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.

Again, a brief reference is repeated that you can’t use tabs, but use 4 spaces. And hereafter, the most interesting is reading a footnote to this point.
Nb: Using only spaces, and not mixing spaces with tabs, helps to avoid problems with diffs, patches, history, and annotations. The use of spaces also makes it easy to insert fine-grained sub-indentation for inter-line alignment.

We use Google-translator:

«of Nb: Use only spaces, not mixing space with tabs, it helps to avoid problems with diffs, patches, history and annotations. The use of spaces also makes it easy to insert a fine-grained sub-indent to align between the line. ”

With the indent, it’s quite an interesting feature, but I’ve never used it, it’s used like this:



But this is precisely what SmartTabs theory says, when tabs are used to indent from the beginning of the line, and spaces are just for such tricky maneuvers (and when changing the size of the tab, the main code will change and the code with spaces will always remain readable regardless of the size of the tab).

But the spaces, as written in the standard, are used so that there are no problems when working with version control systems. This is what I want to understand and check if this is so.

We put experiment No. 1


1. Create two files, in one there will be a code with spaces in the other, exactly the same code, but with tabs.

2. Make copies of these files and make changes to them.

3. Now let's look with the WinMerge program

Tabs



Spaces



4. Send these files to GIT

5. Let's look with the program SourceTree

Tabs



Spaces



6. Let's look at the Bitbucket website



7. As we can see with a regular, non-repeating code, there are no problems, spaces aren't important or tabs.

We put experiment No. 2


1. Now let's put an experiment, the bugs of which I myself have repeatedly noticed using tabs. It’s very interesting to see if the gaps really solve this problem.

2. Create two files, which after the changes will have repeated pieces of code. And also make copies of these files and make changes to them.

3. Now let's look using the WinMerge program

Tabs



Spaces



4. Send these files to GIT

5. Let's look using the program SourceTree

Tabs



Spaces



6. Let's look at the Bitbucket website



7. Suddenly, with the tabs that with the gaps the problem is visible to the naked eye and none of the programs could correctly understand where the changes occurred. Then why in the standard it is written that spaces allow to solve a problem: helps to avoid problems with diffs, patches, history, and annotations.

In conclusion


So it may be worth spitting on this point of the standard and using SmartTabs, because the advantages of using tabs at the beginning of the line are undeniable. Tabs can be configured as you like, you want as two spaces, you want as 4, but you want as 8 or even 3. At the same time, if everything is used correctly, the code will never go anywhere.

UPDATE1
After reading the comments, I completely changed my opinion about the gaps, after conducting this study I wanted to understand the pluses of 4 spaces in front of the tabs, especially since it seemed to me that the tabs have much more advantages. But now the tabs no longer seem as good as before. Yes, you need to do as in the standard, especially since most do it, why swim against the tide. But spaces also have their drawbacks. In general, when they did the standard, they chose the least evil of the two possible (IMHO).

But these holivars seem to be going on for quite some time.

UPDATE2 10/14/2015
At the request of a comment, I will gather in one place the main points why spaces are better than tabs:
  1. SmartTabs is certainly great, but tzlom wrote in his comment , in which case even smart tabs are worse than spaces.
  2. Commonplace, most people write spaces, there just so happened historically ( comment ) from the user symbix , and that's proof

The advantages are certainly strained, but only those who participated in the development of the standard know the whole truth about the causes of gaps against tabs.

Also popular now: