Best Practices
Best Programming Practices¶
Like any other language, R has syntax conventions that it is not mandatory to follow in order to get the code working but make your code readable, primarily by yourself as well as by others.
On the one hand, don't be presumptuous: unless you're an alien, you won't be able to understand your R codes in 2 months or even next week if you do not comment on them, extensively stating your purposes and your algorithmic options.
Always try to apply to the form of your code the same logic that governs its content. This is key to its readability.
Keep also in mind that your sense of readability is rarely that of others. This is why it is necessary to comply with rules that are developed by a large community of developers and generally accepted by consensus.
For a nice summary of good practices in R coding, please see the section 10 Coding Best Practices of "R for Epidemiology" by Brad Cannell.
You can also check the sections 2.4 Reading and writing Code and 4.3 A brief style guide of the Nathaniel D. Phillips's guide and the Style guide of Hadley Wickham which gives good and bad examples of R coding practices.
As a last word, we admit that the coding rules in R are less precisely described than for other languages (Python for example, not to name it). If how to code an R instruction seems ambiguous to you, look at what others are doing (StackOverflow is your friend) and choose the style of the majority!