What is Code Refactoring?

One of our core tenets of development is that code is read more than it’s written. To that end, we must make our code as easy to read and understand as possible. No one writes perfect code on the first try so it’s important that we continually refine our code so it’s easy for the next developer to read even if we’re the next developer.

In this article, we’ll discuss what refactoring is, how to make it dead simple to do, and discuss the next set of articles in this series.

What is Refactoring?

Code Refactoring, or just refactoring from this point on, is the process of restructuring existing code without changing its external behavior.

When we refactor our code we’re primarily looking to do two things.

  1. Improve Readability
  2. Reduce Complexity

These two items help make the code easier to maintain and extend which allows us to deploy more features to our users.

Unfortunately, refactoring can be a very subjective process so when we’re working on a team it’s a good idea to make sure that as we refactor our code it still fulfills the standards that the team has created. Maybe run the options past a co-worker to see if they agree with the best course of action.

How To Easily Refactor Our Code

The most important component to being able to easily refactor our code is automated tests. Having automated tests allows us to confidently change our code and know for certain that we didn’t break the code we just refactored. We can still refactor our code but we have to be a lot more careful about it.

The best way to make sure that our code has automated tests that will detect the happy accidents made during a refactoring is by developing our code using Test-Driven Development (TDD). For a refresher on TDD check out our articles on the subject.

  1. What is Test-Driven Development?
  2. Test-Driven Development with PHPUnit

Now we need to look for some code that needs to be refactored.

The way that we do this is by looking for code smells. Things about our code that make us look at it and go P U.

There are lots of different code smells. Some of the easiest to find are:

  1. Long Methods
  2. Duplicate Code
  3. Poor Names
  4. Long Parameter Lists
  5. Dead Code

It’s really easy to find long lists of code smells on the internet so feel free to Google for them. In future articles we’ll go over some different code smells and what we can do about them.

Refactoring by Martin Fowler

An excellent primer for refactoring our code is “Refactoring” by Martin Fowler. Available at any place that sells books.

We highly recommend purchasing this book and giving it a once-over to get some of that information into your head. In this series we’ll go over a lot of the refactorings described in the book adapted to work in a PHP-friendly context.

What You Need To Know

  • Refactoring: the process of restructuring existing code without changing its external behavior
  • Code Smells: things to look for in our code to determine where to refactor