[This article was first published on rapporter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 withTAB
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?
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.
Installation
Well, almost automatically. Your contributors have to install EditorConfig first. This is pretty easy on any operating system, and beside binary packages build from C code, there are also Java, Python and even JavaScript versions available to download.
Plugins
After installation, you have to make sure that your text editor or IDE supports EditorConfig:
Unfortunately, RStudio support is not available at the moment as far as I know, but I hope to see this functionality soon integrated in the most trending R development tool – probably with some sane default config file automatically added to each R project.
Configuration
Just add an
.editorconfig
file 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
There we simply define the default coding style for all the files in the repository:
- the code should be indented by 4 spaces,
- using Unix-line
lf
line breaks, - 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.
Then we override the number of spaces in the C++ sources to be only 2 instead of 4. Simple and clean, huh?
Testdrive EditorConfig
I’ve just added the above file to my pander R package and kindly asked Emacs to force the style on the sources. The result was pretty impressive: around 750 lines were affected – despite the fact that I always tried to keep the code base clean and pretty with a few contributors.
Now, if anyone forks my repository and edits the files in an EditorConfig-compatible text editor/IDE, my default coding style will be used automatically by the editor, despite the fact how the contributor usually writes code.
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!
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!
To leave a comment for the author, please follow the link and comment on their blog: rapporter.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.