When should we write functions?


#1

Hi community !

Actually, it is not a question but more like a tip. Some people may wonder when should we write a function while programming. For my part, I always remember one of @hadley rule of thumb that I read from the book R for Data Science:

You should consider writing a function whenever you've copied and pasted a block of code more than twice.

That being said, another goal like that and we should write a function:

I wish you handsome R functions and also great NHL playoffs !

Sté


#2

Function in question is clearly not pure - so many side-effects :slight_smile:


#3

You might also want to create a function to pass a bunch of code to one of the purrr functions (or a loop if you aren't into functional programming). Though, I guess that is also a way to avoid repetition...