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.
- Improve Readability
- 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.
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:
- Long Methods
- Duplicate Code
- Poor Names
- Long Parameter Lists
- 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
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows