Do not Repeat Yourself – DRY rule
vs. Write Everything Twice – WET rule
Following the WET rule:
- Makes changes more difficult and/or time consuming.
- Decreases code clarity.
- Increases the likelihood of inconsistencies.
To prevent duplication and follow the DRY rule, we can write custom functions.
Functions are ‘self-contained’ sets of commands that accomplish a specific task.
Functions usually ‘take in’ data or parameter values (these inputs are called ‘function arguments’), process them, and ‘return’ a result. You’ve already used several functions in this tutorial; for example rnorm(n, mean, sd)
, where n
, mean
, and sd
are inputs and the result is a random sample from the normal distribution. The only difference here is that you are writing the function yourself. Once a function is written, it can be used over and over again by calling its name, just like other functions such as rnorm()
.
To write your own function, use the function function
:
<- function(argument1, argument2,…) {
AwesomeFunctionName
do stuff here }
To build up a function, start by writing the “stuff
” outside the function to test that it works.
YOUR TURN:
1. Create a function that repeats the calculation of the mean of 100 values drawn from a standard normal distribution (use mean(rnorm(n = 100))
for this calculation) nrep
times and returns a histogram of the nrep
means. 2. Modify your function such that, in addition to the number of repetitions nrep
, the number of drawn values n
(i.e. argument n
of the rnorm()
function) can also be varied when calling your function.
Note that it is useful to define the number of repetitions nrep
outside of the function, so users of your script can more easily change that value, e.g. from a low number (to verify the script runs without error) to a large number (to obtain reliable results).