What is Refactoring? || 3-Minute Thursday
You’ve heard the term. You’ve seen it in a commit message. Maybe you’ve even been asked to do it, but what exactly is code refactoring??
Have you wondered what refactoring means? Or maybe you know what it is, but aren’t sure how to explain refactoring concisely.
3-Minute Thursday is a new series where I answer your burning code and career questions on your journey from junior to senior developer.
What is Refactoring?
You’ve heard the term. You seen it in a commit message. Maybe you’ve even been asked to DO it. And you’re vaguely aware that it has something to do with improving your code. But what exactly is refactoring?
Hey, this is Step Aument. And this is Three-Minute Thursday.
Refactoring is a software development practice. It’s something you do to code.
Refactoring, or re-factoring, implies that we are changing the existing factorization of our code. But, what is our code’s factorization?
Do you remember the concept of a factor from math?
If you look at this equation: five times four equals twenty, twenty is the product, four and five are the factors. In other words, they are the component parts that together produce the result.
But twenty can be composed of other factors as well. Ten and two are factors of twenty. And we can break either of these equations down into twenty’s prime factorization, five times two times two.
So notice that if I have five times four, I can refactor that into five times two times two, which I could then refactor into ten times two. These are interchangeable.
The structure of the equation has changed, but the outcome - or external behavior has not changed. All three of these equal twenty.
So that’s math, but what about code? What is the factorization of code that we change when we refactor?
Just like with the math equation, the factorization of a piece of computer code is its structure - the composition of the component pieces that produce the code’s external behavior.
There are usually lots of ways to make a computer do something.
I’ll show you what I mean.
These two bits of code are functionally identical, but they’re structured differently.
As a software developer it’s your job to weigh these solutions to come up with an appropriate approach for any given piece of code.
So, refactoring is changing the structure of code without changing its external, observable behavior, hopefully making it more maintainable in the process.
If you want more concrete examples of refactoring, be sure to visit my exercism.io walkthrough series, linked in the description below, where I do a ton of refactoring.
If you want a deeper dive on how to approach refactoring so you can write code today that you won’t hate in 6 months and become a hero to your team and add massive value to your company, check out my tactical refactoring video course also linked in the description below.
Thanks for watching, and I’ll see you in the next video!