by Guangming Lang
1 min read

Categories

  • r

I didn’t pay much attention to code style until recently when I had to collaborate with a couple of guys on a project. That experience taught me having a consistent style across all team members can greatly reduce communication time and help people understand each other’s code. If you’re not already using a style, you should decide on a style today. Even if you program solo, you will also benefit from sticking to a style. It will allow you to write code faster, for example, once you choose a style, you don’t have to spend time deciding whether you should use all lowercase letters chained by dots or undersores or use camelCase everytime you write a variable name. It will also make your code easier to read.

The best way of choosing a style is to adopt from the existing ones. We adopted Goolge’s R style with some modifications. Notably, we use = instead of <- for assignment since we don’t see any practical harm of using = over <-12 and it’s a bit harder to type <-. At the moment, I’m only aware of one occasion where <- and = are not interchangeable, and it’s when used in the substitute() function.

# <- works
substitute(x <- x + 1, list(x = 1))
## 1 <- 1 + 1
# but = runs into error
substitute(x = x + 1, list(x = 1))
## Error in substitute(x = x + 1, list(x = 1)): unused argument (x = x + 1)