# How long is a function?

- -

Within the R project and contributed packages, how long do functions tend to be? In our experience, people seem to think that functions are only needed when you need to use a piece of code multiple times, or when you have a really large problem. However, many functions are actually very small.

R allows a lot of “computation on the language”, simply meaning that we can look inside objects easily. Here is a function that returns the number of lines in a function.

This works because deparse converts an object back into text (that could in turn be parsed):

so the function.length function is itself 6 lines long by this measure. Note that the formatting is actually a bit different, in particular indentation, braces position and spacing is different, following the likes of the R-core style guide.

Most packages consist mostly of functions: here is a function that extracts all functions from a package:

Finally, we can get the lengths of all functions in a package:

Looking at the recommended package “boot”

I have 138 packages installed on my computer (mostly through dependencies – small compared with the ~4000 on CRAN!). We need to load them all before we can access the functions within:

Then we can apply the package.function.lengths to each package.

The median function length is only 12 lines (and remember that includes things like the function arguments)!

The distribution of function lengths is strongly right skewed, with most functions being very short. Ignoring the 1% of functions that are longer than 200 lines long, the distribution of function lengths looks like this:

Then plot the distribution of the per-package median (that is, for each package compute the median function length in terms of lines of code and plot the distribution of those medians).

The median package has a median function length of 16 lines. There are handful of extremely long functions in most packages; over all packages, the median “longest function” is 120 lines.