And it's important to note again, those tests must cover the existing behavior. If we are fixing a bug in a piece of code, but we decide to refactor it first, then those tests should cover all the existing behavior, even if it's wrong. Because if we are changing the test and the code at the same time, then we can't know if the refactor worked. We need to write the tests that ensure that the code does the ensure that function does the same thing once we've refactored it. And only then do we change the test and change the code to fix the bug. And then at that point we can refactor.
For the last part of the talk, I wanted to go over a few little tips that are going to help us refactor code in the face of this idea of complexity and understandability. Cognitive complexity, quite obviously, as a score, punished nested code. The more things we put onto the brain stack, pushed onto the brain stack, I suppose, the more complex things got. And so, reducing nesting is going to help you deal with that and just pop some of those things off of the brain stack and leave you with less to consider as you read through a function.
The things we're going to cover for this are inverting conditions and exiting early, structural collapse, extracting helper functions and just a couple of little JavaScript features that are going to help us out with this as well that are a bit more modern. And so, inverting conditions and exiting early. This goes to the first thing I showed you in that insert many function in which I said there was this big conditional around the outside that's checking that the length of the document's array is greater than zero. Otherwise, it just kind of returns an empty object, really, an inserted count of zero. This big condition around the function that then has everything else inside it, obviously, increases that nesting. And if we invert the condition, that is if the document length is zero or less than zero, I guess, it's not really going to happen with an array. But if the document length is zero, then we can immediately return our kind of zero object and then discount the idea that this is a problem anymore. The function can then just carry on just safe in the knowledge that we are dealing with a list of documents and that's fine. Inverting and exiting early just, yeah, pops things off the brain stack and means we don't have to consider that later on in the function. Just pop it off the brain stack. That's what I'm going with.
Structural collapse is sort of similar. We're trying to reduce the number of conditionals, especially the nesting conditionals, if we can kind of squish them together. And so, in this case, insert many actually can deal with an options document, options object that may have an array of vectors, and then it's going to try and zip those vectors and documents together. But if the length of the vectors and the length of the documents isn't the same, then that's an error, because they're not they're not going to go together. But this is like nesting conditionals here. And what we could do is squish that kind of thing together to check that if we have vectors and if the vectors are not equal to the same length as the documents, then we can throw an error. And so, this actually allows us to, in this case, return early via an error rather than just carrying on nesting further and then we can let the rest of the code carry on. Squishing those kind of things into one conditional allows us to, yeah, consider it dealt with a lot quicker and just pop it off the brain stack.
Extracting helper methods, I think, is actually the most useful thing here.
Comments