In the past 10 years since I started using R, there were a series of things that changed the way I code (in chronological sequence): my first R function, different IDEs, my first R package, roxygen, GitHub, the devtools wiki, StackOverflow, my first useR! conference, continuous integration tools and e.g. participating in the Google Summer of Code.
Probably I will blog about this exciting journey later, like moving from Notepad++ to Eclipse/StatET to Emacs/ESS, but now let’s concentrate on some new cool stuff that improves collaborative coding and development: EditorConfig, which tool definitely extends the above list.
Did you ever get a pull request on GitHub with outlandish code style?
I truly feel for you. Getting some code chunks polluted with
TAB characters or using 2 instead 4 spaces for indentation can be really disturbing, and manually cleaning up the patch is more than tiring. And we all hate those chaotic commits full of removed trailing spaces and other automatic updates of some random IDE that makes the list of changes unreadable, right?
Although most GitHub users are willing to adopt to your coding style while checking the original code base to prepare their pull request, but:
- will they really find out if you like soft or hard wrapped lines?
- would you come up with your own coding style referenced in all your R repositories?
- or should everyone use the same code style enforced by some R authority?
None of these will happen.
Let your contributors adapt to your style even without a single click!
Thus, you’d better create a config file in your repository that describes your coding style and supports most text editors and IDEs automatically. Editors, which will comply with your style on any machine or platform, without any manual intervention. It’s not just a dream any more:
EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.
.editorconfigfile to your repository with e.g. the following POC content to demonstrate some of the nice features of EditorConfig:
## IDE-independent coding style via EditorConfig: http://editorconfig.org/ ## Please be sure to apply these when submitting a pull request. root = true [*] indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.cpp] indent_size = 2
- the code should be indented by 4 spaces,
- using Unix-line
- the character encoding should be (of course) UTF-8,
- do never leave trailing spaces at the end of the lines,
- and do not forget to insert a blank line at the end of the file.
This is a real magic, and a must for any larger project with more than one contributors: you will never ever have to comment on a pull request to remove the Windows-style line breaks from the patch!