Compare this to the universal dominance of particular styles in other languages like Ruby and Python, and it seems bizarrely egalitarian. Is the JS community particularly fractious and disorganised?
The top ten projects tell a different story. Eight of them use two spaces (jQuery and three.js both use tabs.)
How do the results compare with advice from the most prominent styleguides?
- Douglas Crockford: four spaces
- Google: two spaces
- jQuery: tabs
- GitHub: two spaces
- NPM: two spaces
- CoffeeScript output: two spaces
The prevalence of the two spaces style is probably a result of the influence of the Ruby and Rails on web development, and of the callback-driven nature of browser / Node.js development. But I’d love to hear other suggestions as to why.2
Indentation is a matter of personal preference (it’s why they call it style, after all), but if you’re starting an open source project and want it to be consistent with community standards, indent with two spaces.
Join the discussion on Hacker News
Projects on GitHub might not reflect how most programmers (who work on commerical software) are writing their code. But seeing as most production JS is minified beyond recognition, it’s the best dataset we’ve got to work with. ↩