The Truth About Refactoring Code

Where do I start? 😭😭😭

¡

5 min read

Refactoring code is a journey every developer must face at some point, whether you're looking at a tangle of your own making or grappling with someone else's mysterious creation. I find myself asking the same questions every time: Where do I begin? Why is this so hard? And WHAT IS THE POINT? As I’ve continued to write code, more and more people encourage me to refactor my code. So, I set out on a journey to answer these questions. Here’s what I found:

What is refactoring code?

Before we talk about how to refactor code, let’s establish a definition. I would define refactoring very simply as the act of cleaning up code without changing its external behavior. Refactoring often involves:

  • Renaming variables to make them more descriptive.

  • Breaking large, unwieldy functions into smaller, more manageable ones.

  • Simplifying complex logic to improve readability (AKA the bane of my existence)

  • Removing redundant or unused code.

It’s NOT about adding new features or fixing bugs. If that’s the case - if the code already works, you might be asking yourself, why bother?

Why refactor? What’s the point?

There are a few key reasons to refactor code such as improved readability, easier maintenance, and enhanced performance (While refactoring isn’t always about optimization, simplifying logic can sometimes lead to faster, more efficient code. Regardless…). We can sum all these reasons into one simple objective; making the code easier to work with in the long term. It’s an investment where less is more; clean, succinct code today means fewer headaches tomorrow.

But I already knew all this! - well before I set out to write this blog post. Still, I could not find the motivation to refactor my code, even as I prepare to submit job applications. Not even the incentive of impressing potential employers could outweigh my frustration toward the task of refactoring. Then, during a class assessment, I was asked to debug the following code (I know I just said refactoring isn’t about fixing bugs, and it’s not! bare with me, for just a moment):

Story Time :)

my_numbers = [1,2,3,4,5] 

for number in my_numbers: 
    print(number) 
    if number % 2: 
        print("even") 
    else: print("odd")

I was told the above code should print even for even numbers and odd for odd numbers (easy enough). As written above, the code prints even odd even odd even, the opposite of what’s expected. I knew that if number % 2 is the same as writing if number % 2 == True. Then, I quickly discovered % (the modulus operator) does not return division’s quotient; it instead outputs the remainder. Therefore, a remainder of 1 (AKA True) prints even, while a remainder of 0 (AKA False) prints odd. I begrudgingly proposed the following solution to debug the code:

if not number % 2:

Even though my solution returned the correct output, I didn’t like how the code appeared to human readers- the way it is written isn’t easy to understand. For those interested, a more readable solution is if number % 2 == 0:, but the specific solution isn’t the point! In that moment, I was reminded:

Code itself is a compromise—a delicate balance between human and machine languages. Computers, at their core, speak in binary: sequences of 0s and 1s that are precise but close to incomprehensible for the human brain. Meanwhile, humans think in rich, expressive languages that are far too complex for machines to understand. Code lives at the intersection, a beautiful dance along the line dividing these two worlds.

Refactoring is the practice of perfecting that dance—making the steps smoother, more graceful, and easier for everyone (humans and machines) to follow. I realized refactoring code is important for all the obvious reasons; PLUS, it helps me more deeply and intimately understand the machines I’m working with, ultimately, allowing me to work in more effective and meaningful ways.

Finally, when should you refactor code?

As much as I’ve sold the value of refactoring code, I cannot deny refactoring code is A LOT of effort. And I don’t think it’s always worth the effort…

If you ask google, chatGPT, or whatever your search engine of choice “when is it important to my refactor my code?” You’ll receive a response along the lines of “If your code is about to handle more users, data, or functionality, refactoring can lay a solid foundation for future growth.” That’s a start but a bit too general to be of much help.

In my first blog post, Coding as an Art, I demonstrate how the same task is accomplished with 3 different blocks of code and then, discuss why each method might be interesting and valuable to potential employers. This is to say what you do with your code depends on the context you’re working in; primarily, what your team or employer values. Get on the same page as your collaborators. Communicate - do not invest time in something that will not pay off. Watch and listen. How do people work (or struggle to work) with your code? Even better, actively solicit that feedback. If you have a supervisor, and you’re struggling to refactor, ask if refactoring is a current priority. When you’re working collaboratively, you have plenty of factors and resources to determine when to refactor.

If you are coding all on your lonesome, you can simply ask yourself, “does my code look like a Choose Your Own Adventure novel on steroids?” If yes, refactoring can help. Ultimately, the choice is yours. Just remember: Refactoring makes your life easier in the long run.

Â