Are there objective reasons for using spaces instead of tabs for indenting files as per PSR-2 standard, can someone provide:
on which PSR-2 standard is based?
Authors of PSR-2 standard had in mind something more than "look and feel", something more than just opinion based thing, and lots of people have trouble understanding why spaces are better during teamwork.
Based on my experience, we faced on our projects: GIT and other version control 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.
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-indentations for inter-line alignment.
This subject has been covered in depth by Google’s Felipe Hoffa on Medium. Felipe analyzed 400,000 GitHub repositories, 1 billion files, and 14 terabytes of code to determine why spaces might be better than tabs. It was a clear win for spaces over 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). That's an old survey and might be misleading, it's just a true statistical fact when the answer was originally written :)
If every collaborator on your project would keep the same standards on coding - it would be good in the long run, collaboration is more efficient and professional, the same indent when you refactor or develop. Studies regarding that:
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.
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.