Search code examples
javarcoding-style

How to apply Clean Code to R and what are some alternatives specific to R?


I self-learned R in a really poor way and I’m trying to improve. I’ve read Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (2009), which provides best practices for coding and I found it helpful. However, my capacity to understand it was limited because I have no programming experience besides R so couldn’t understand the jargon (or much of the examples, written in Java). My question is:

  1. In the attached image, I’ve listed terms from Clean Code that I didn’t understand and made a guess at what the equivalent would be in R terminology - could you please correct me if I got any of these wrong? (I did buy the book, but for the purposes of this question, a pdf of Clean Code is found here: Clean Code pdf)

If I found other books that provide foundational best practices like Clean Code does that are written specifically for/with R, it would really help. I use R for data science and I never get very far into a project without creating a total rat's nest; in particular, I struggle with writing concise code, and in knowing when I should start a new script, and how to get separate scripts to work together coherently.

I have also read Good Enough Practices for Scientific Computing by Wilson et al. (2017), which is great. I do use R projects. Thank you for your time!

Clean Code terminology and my attempt at applying it to R


Solution

  • R is a multi-paradigm language, so directly adapting OOL-like code style to improve your R skills isn't exactly the best idea. 16 of the usual GoF design patterns are irrelevant or trivial in FP (hence also in R) - it would be more than counterproductive to limit yourself with the OO mindset.

    I do recommend reading "Clean Code" (the cosmetics of OO code isn't much different than what FP programmers would consider "clean"), but to really optimize your R code you need a supplement of FP. In that case, plenty of books are available for you to read: Michelson's "An Introduction to Functional Programming Through Lambda Calculus" (if you are completely new to FP), "Functional Thinking" by Neal Ford, "Becoming Functional" by Joshua Backfield.

    FP is pure untapped power which you can use in R. Why limit yourself only with OOP?