So the last, thing I want to talk about is to limit the length of your functions.
Alright, so functions in R can,
can theoretically go on for quite a long time
and, of course, just like in any other language.
But just like in any other language, I think the logical thing to
do with the function is to limit to kind of one basic activity.
So, for example, if your function is named, read the data.
Then your function should simply read the data.
It should not read the data, process it,
fit a model, and then print some output, alright?
So you should, the logical kind of steps like that should probably
be split into separate functions.
There are a couple of different advantages to doing this.
First of all it's nice to have a function written on a single page of code.
So you don't have to scroll endlessly, to see
you know where all the code for this function goes.
If you can put all the function, the ent, the entire function on like, one screen of
the editor, then you can look at the whole function and see what it does all at once.
Another advantage of splitting up your code onto logical sections, to logical
functions is that if you use functions like
trace back or, or the profile or the debugger.
These often tell you, you know, where in the
function call stack you are when a problem occurs.
And if you have multiple functions that
are all logically divided into separate pieces.
Then when a bug occurs and you know it occurs in a certain type of function,
or a certain function, then you know kind of where to go to fix things, right?
So if you, but if you just have a single
function that just goes on forever and a
bug occurs, then the only thing that the debugger
or the traceback or the profiler can tell you
is that there's a problem in this one function.
But it doesn't, it's difficult to tell you where exactly the problem occurs.
So splitting up your functions has a secondary benefit which
is that it can help you in debug and profiling.
So limiting the size of your functions is
very useful for readability and for kind of debugging.
Of course, it's easy
to go overboard and to have, you know, 100 different three line functions.
So that's not really what you want to do.
So you just want to make it so that the, the separation of different
functions is logical and each function kind of does does one thing in particular.