Objective reasons for using spaces instead of tabs for indentation?

后端 未结 1 647
陌清茗
陌清茗 2021-02-19 13:02

Are there objective reasons for using spaces instead of tabs for indenting files as per PSR-2 standard, can someone provide:

  • facts,
  • references,
  • sp
1条回答
  •  终归单人心
    2021-02-19 13:43

    Facts:

    1. GIT and other version controls systems treat white-space differently

    Based on my experience, we faced on our projects: GIT and other version controls systems treat invisible spaces + TABS differently, and it leads to changes in lines, which actually haven't been affected. It's easy not to notice, when there will accidentally added one space + TAB = indent looks the same in IDE, but GIT will make the difference when merging. It damages your ability to effectively compare revisions in source control, which is really scary. It never going to happen when you are having spaces only.

    2. Neutralize difference in collaborator's environment (Editor, OS, preferences, etc.)

    The tab width (in spaces) depends on your environment (text editor, OS, preferences, etc.), but the space width is the same everywhere. IDEs are smart enough to treat white spaces up to your personal taste, but the output generated for collaboration should be up to standards. As PSR-2 states, 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.

    3. Developers who use spaces make more money than those who use tabs

    Using spaces instead of tabs is associated with an 8.6% higher salary. Using spaces instead of tabs is associated with as high a salary difference as an extra 2.4 years of experience. (source: Stack Overflow 2017 Developer Survey).

    4. Numerous studies on coding style importance

    If every collaborator on your project would keep the same standards on coding - it will be good in the long run, collaboration is more efficient and professional, the same indent when you refactor or develop. Studies regarding that:

    1. For example, Ben Shneiderman confirmed this in Exploratory experiments in programmer behavior:

      when program statements were arranged in a sensible order, experts were able to remember them better than novices. When statements were shuffled, the experts' superiority was reduced.

    2. An old 1984 study by Soloway and Ehrlich cited in Code Complete, and supported studies from The Elements of Programming Style:

      Our empirical results put teeth into these rules: It is not merely a matter of aesthetics that programs should be written in a particular style. Rather there is a psychological basis for writing programs in a conventional manner: programmers have strong expectations that other programmers will follow these discourse rules. If the rules are violated, then the utility afforded by the expectations that programmers have built up over time is effectively nullified.

    0 讨论(0)
提交回复
热议问题