To keep code elegant and readable, this lesson presents a refactoring technique consisting of two rules: (1) Don't have empty lines in a function, (2) keep the function short, with at most 6 or 8 statements. See how this can help keep each function focused on one responsibility.
The second rule is to keep functions short, with at most six statements or lines of code. Usually in a function that has a lot of statements you feel like putting this space here, to separate these parts. This is really common, but the technique I follow is that I never put these empty lines. Then what you may think is that it gets quite nasty, once you have a lot of statements, right?
For instance in this function here, animate, it has a lot of statements. If you take a look here, it has 40 lines of code, so it's pretty large. It has no spaces, empty lines in between. It gets quite nasty. As you can see, it's quite nasty.
What I do instead, is I try to refactor this animate function to be really small. We can do that by moving some of these parts away.
For instance, I can move this one away. We're going to put that in a function. We're going to call it determineDeltaPoints, which takes this argument. I'm going to return that, and then I can call that function here.
This is, by the way, RxJS code. I'm just going to call that like this, and then I can move this part out as well. I can name it as expandAsRenderingFrames, and then I can call it as well.
There's this big scan here which I can also move, and I can put it somewhere else. Let's put it in a function called, calculateAnimationSteps. It takes an accumulator, and a current point.
Actually it takes...no. It takes a point stream. Put in all of the scan inside that, and then I also call that function as well.
There you can see now animate is a really small function. It has only these steps, and it's really readable. It doesn't have any spaces, any empty line in between, because it's really short. If it's short, then it doesn't need any empty lines.
That's the general approach that I follow in. As you can see, we made some other functions here. You could even decide maybe to put this one in a function, and then you would make the determineDeltaPoints even shorter.
Of course, this one is also quite huge. It doesn't have empty lines in between, so we can also refactor this one if you want.
We can get this, and we can move it somewhere else. Let's call this one incorporateNewpoint, with an accumulator and a current point. I'm going to return this new accumulator, and then you can call that from here. You could also ask yourself, "Is this function quite short?" Or do you feel like putting an empty space?
The idea is that if you feel like putting an empty space, then it probably means you should make this function even shorter by refactoring or splitting it in another function. As you can see here as well, this is another part that we can take out. I'm going to call this function progressEachPoint, takes an old accumulator and a new accumulator.
It returns this progressedAccumulator, and then I'm going to call that function here. Now as you can see, this calculateAnimationSteps with this scan operation is much more smaller, much shorter. I don't really feel like putting an empty line here. Maybe I could even extract this one as a function.
The idea is that if you feel like putting an empty line in between statements, then probably what you need is just to split that function in more functions. I've been following these two rules, and they've been really helpful for me to keep all of my functions quite short.