The next re-factoring which is also the last one that we'll
see, extract method is one of the most commonly used re-factoring. As
it is applicable in many, many situations. The starting point is
a method that is too long and contains cohesive code fragments, that
really serve a single very specific purpose. So we start from
a cohesive code fragment in a large method. What we can do
in this case, is to create a method using that code fragment.
And then replacing the code fragment with a call to that method.
Let's look at this with an example. Here over this method called
print owing, and what it does, imagine that it does a lot
of operations here that I'm just not listing, and then it's got
a set of print statements. That are just printing a lot of details
about the owing information. And then again, a lot of code after
that. So what I could do in this case to simplify. The
method is to transform this set of statements. They are cohesive in
the sense that they do just one thing, they just print these details
into a method, and then I had, replace the statements with a
call to that method. Which is actually something similar to what we did
as part of some the previous re-factoring's. Here I'm showing the result.
So here is the method that I extracted. As you can see. It
contains the code that was previously here. I give you the meaningful
name, I called it printDetails so it's clear what it does. And now
the print owning method is simpler. Because I still have the remaining code
the one I didn't touch. But now this potentially long list of details.
Of prints, of details is not replaced by a single method code.
So a gain similar to the previous refactorings that we saw. If
we just look at the printing method, it's very easy to figure
out what this part does. Oh, print some details. And once more I
really want to stress this. If you don't care about how, this
is implemented and knowing that this print some details is enough. Then
you're done. You don't need to understand anything more. It's clear, it's
self explanatory. And if you'll need to look at what print details does,
you just go and look at print details. And you look at
it in isolation. So it's easier to understand what this does without having
to think the rest of the code. So once more the color we
factor in is just to improve your design, made the code more readable
Make the code more maintainable. And also keep in mind all of these,
are kind of small examples. You also always have to think about the
effect that this can have on larger codebases. It can really improve a
lot. The understandabililty, and maintainability of
your code. So in general, it's design.