We saw at the beginning of the lesson, what are the
goals of refactoring? Or what are the reasons ,why we need to
refactor in the first place? The first reason is that requirements
change, and when the requirements change, we often need to change our
design accordingly. In other cases if any of the requirements unchange,
we might need to improve our design. And this happens for many
reasons. For example, we need to add a new feature, we
want to make the code more maintainable, and also in general programmers
don't come up with the best design the first time. So they might
need to adapt it after the fact. And the final reason I want to
mention is sloppiness, and to some
extent laziness, of programmers. And a typical
example of this is something that we all have done, which is copy
and paste programming. So instead of rewriting a new piece of code, because
we know that there is some code in some other parts for the
program that does a similar thing, we'll just copy the code over. And
before we know, we end up with tons of copies of the same functionality.
And when that happens, a good way of consolidating that code and
extracting that functionality is to use refactoring, for example, by creating a
method or a class that provides the functionality. And we'll see specific
examples of that. A question I would like to ask at this
point of the class is whether you have used refactoring before? So
I want you to take a second and think about it. And
no matter what you're history is, if you ever coded I bet
you any money that the answer is yes, you have done refactoring.
What do I mean? I'm going to give you an
example. I'm sure you renamed the class or a
method or change the name of some variables in
the code before. That's refactoring. Even something as simple as
renaming a class is refactoring, because, for example, it
might help you making your code more understandable. And of
course I'll admit that in this case, this is
a trivial refactoring, and there are much more interesting ones.