Nov 01, 2024
As a product engineer, I've discovered how generative AI is transforming my code refactoring process. I'm not claiming AI is a cure-all, but I want to share a practical example that many developers facing messy component states will understand.
I was working on a multi-step form component "PinContent" when I realized my simple implementation had become a maze of useState declarations and if statements.
This is a common situation - requirements evolve, and our components grow messier over time. I needed to add a new step between user validation and profile picture selection, leaving me with two options:
Here's a critical tip: Always commit your working changes before starting any AI-assisted refactoring. I've learned this lesson through experience. Without a committed baseline, you risk losing your last working state if the AI suggestions don't pan out.
I currently use Cursor with Claude Sonnet 3.5. While the tool offers various features, I've found Composer handles 75% of my AI interactions, especially for major refactoring work.
Here's my process:
Cursor presents changes as a diff, similar to a pull request review. This familiar format makes the process more intuitive and manageable.
Across different AI models, the suggested approach remains consistent:
The AI sometimes misses important details. For example, it struggled with TypeScript's type guards in my recent refactoring. I had a cashTag variable (string or null) with specific states guaranteeing non-null values.
The AI's initial refactoring broke these guarantees.
So here is what I do instead:
This process works about half the time - not perfect, but valuable given the task complexity.
This particular refactoring would typically take me 5-10 minutes with careful attention. Using AI, I completed the same task in about 30 seconds, plus 1-2 minutes for fixing type errors. Even with some manual fixes needed, the time savings are substantial.
AI tools like Cursor are reshaping both our speed and approach to software development problems. They complement human judgment with fast prototyping and refactoring capabilities.
Looking ahead, I wonder how these tools will evolve to better understand our code's implicit guarantees and patterns. The AI-assisted development journey is just beginning, and its potential is exciting.