Issue #2June 26, 20255 min read

How to Refactor Coding Agent's Code

refactoringlegacy-codellmcode-qualitycraftsmanship

Last issue, I argued that refactoring skills are more valuable than ever in the age of coding agents. Today, let's get tactical. I'll show you exactly how I safely refactor code generated by coding agents.

The Problem with Agent-Generated Code

Coding agents generally produce working code, but it's sloppy. Much like the kind of code I write when making a test go green in TDD. The agent has solved the immediate problem, but the design often suffers.

Here's what I consistently see in agent-generated code:

  • Long Parameter Lists - Functions that take 6+ parameters
  • Feature Envy - Methods that seem more interested in other classes
  • Primitive Obsession - Using strings and integers instead of domain objects
  • Duplicated Logic - Similar code scattered across multiple places

These are classic code smells that signal design problems lurking beneath working code.

Bot Refactoring Protocol

When I get working code from a bot, I never ship it immediately. Here's my process:

1. Review the Diff

I carefully examine what the agent changed or created. I need to understand the solution before I can improve it.

2. Take Note of Code Smells

I scan for the common patterns - long parameter lists, feature envy, duplicated logic. After three years of studying refactoring, these jump out immediately.

3. Remove Comments

Agent-generated comments are usually noise. Good code should be self-documenting. (I do this myself in a JetBrains IDE)

4. Refactor with Test Safety Net

I ensure there's test coverage, definitely at the acceptance test level. Then I start refactoring using specific vocabulary with the agent. If I don't have solid test coverage then I will refactor using automated refactoring tools that are much more deterministic and reliable. The coding agents have improved a lot, but the way they refactor code is error-prone. I'm looking forward to the day Claude Code can use the refactorings tools in IntelliJ via MCP.

Real Example: Parameter Object Refactoring

Let me show you this in action. Recently, a coding agent generated this method:

public void processOrder(String customerId, String customerName, 
    String customerEmail, String productId, String productName, 
    double productPrice, int quantity, String shippingAddress) {
    // ... implementation
}

Instead of accepting this parameter soup, I coached the agent:

Me: "This method has a Long Parameter List code smell. Let's introduce Parameter Objects to group related data. Create a Customer class and a Product class."

The agent immediately understood and refactored to:

Bot:

public void processOrder(Customer customer, Product product,
int quantity, String shippingAddress) {
// ... implementation
}

Much cleaner. But I wasn't done.

Me: "We still have primitive obsession with the shippingAddress string. Let's create an Address value object."

Bot:

public void processOrder(Customer customer, Product product,
int quantity, Address shippingAddress) {
// ... implementation
}

The Key: Speak the Language

Notice how I didn't say "make this better" or "clean this up." I used precise refactoring terminology:

  • Long Parameter List
  • Parameter Objects
  • Primitive Obsession
  • Value Object

The coding agent understood exactly what I wanted because I spoke its language—the language of refactoring patterns.

Beyond Code: Strategic Thinking

This isn't just about cleaner code. Each refactoring makes the next feature easier to add. When the next requirement comes in—maybe we need to handle international shipping—the Address value object gives us a natural place to extend functionality.

"Make the change easy, then make the easy change." - Kent Beck

Your Next Steps

  • Learn the vocabulary - Start with 5 common code smells and their refactoring solutions
  • Practice with your next agent session - Don't accept the first working solution
  • Build the habit - Refactor agent code before you ship it

Remember: Coding agents give us working code fast. But professional developers deliver maintainable code that evolves gracefully with changing requirements. Anything you ship to production you must understand, and refactoring is the best way I know to understand code. That's where your refactoring skills make all the difference.

Have you tried coaching a coding agent with refactoring vocabulary? What happened? Hit reply and let me know.


Want to consistently ship high-quality code that's easy to change and understand? Subscribe to Refactor to Grow for bi-weekly insights delivered to your inbox.

Join the Refactor to Grow community!

Get bi-weekly insights on refactoring, LLMs, and software craftsmanship

Practical refactoring knowledge
Real world agentic coding adventures
Bi-weekly delivery, no spam