Codemods & Refactoring | The Codegen Blog https://codegen.com/blog/category/codemods-and-refactoring/ What we’re building, how we’re building it, and what we’re learning along the way. Thu, 04 Sep 2025 15:38:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 https://codegenblog.kinsta.cloud/wp-content/uploads/2025/07/cropped-Codegen-Favicon-512h-32x32.webp Codemods & Refactoring | The Codegen Blog https://codegen.com/blog/category/codemods-and-refactoring/ 32 32 SWE Agents are Better with Codemods https://codegen.com/blog/swe-agents-are-better-with-codemods/ Sat, 01 Mar 2025 18:00:00 +0000 https://codegenblog.kinsta.cloud/?p=24 Coding assistants like Cursor have brought us into a new era of programming.But there’s a class of programming tasks that remain outside their reach: large-scale, systematic modifications across large codebases. You wouldn’t ask an AI to delete all your dead code or reorganize your entire component hierarchy—the tooling just isn’t there. That’s where codemods come in. A […]

The post SWE Agents are Better with Codemods appeared first on The Codegen Blog.

]]>
Coding assistants like Cursor have brought us into a new era of programming.
But there’s a class of programming tasks that remain outside their reach: large-scale, systematic modifications across large codebases. You wouldn’t ask an AI to delete all your dead code or reorganize your entire component hierarchy—the tooling just isn’t there.

That’s where codemods come in.

A codemod is a program that operates on a codebase, and when you give an AI agent the ability to write and execute them, these platform-level tasks fall below the high-water mark of AI capabilities.

Here’s a real example:
We asked Devin (an autonomous SWE agent) to “delete all dead code” from our codebase. Instead of making hundreds of individual edits, Devin wrote and debugged a program that systematically removed unused code while handling edge cases like tests, decorators, and indirect references.

This modifies over 40 files and correctly removes old code, passes lint and tests, etc.

What made this work?

Devin operates like a state machine: write a codemod → run it through the linter → analyze failures → refine.

Each iteration adds handling for new edge cases until the codemod successfully transforms the codebase.

This mirrors the same cycle developers use for large-scale refactors—just automated.

The best part? You don’t have to blindly trust the AI.

The codemod is a program you can review, run, and verify with linter and test output. You can edit it to add exceptions or improve coverage—much better than reviewing hundreds of individual diffs.


Try it yourself

Want to try this with your own Devin instance?

Install the Codegen CLI:

uv tool install codegen --python 3.13

Then use this prompt:
Download Dead Code Deletion Prompt


Supported Codemods with Codegen

  • Convert Promises to async/await:
    Automatically convert Promise chains to async/await syntax across your codebase.
  • Organize Codebase:
    Move and reorganize code safely with automatic import and dependency handling.
  • Modernize React:
    Convert class components to hooks, standardize props, and organize components.
  • Migrate Tests to Pytest:
    Convert unittest test suites to modern pytest style.

We’d love to hear how it works for you.
Join our community and share your experience developing codemods with Devin or other code assistants.

The post SWE Agents are Better with Codemods appeared first on The Codegen Blog.

]]>
Automating Dead Code Removal with AI and Static Analysis https://codegen.com/blog/automating-dead-code-removal-with-ai-and-static-analysis/ Mon, 16 Dec 2024 21:35:00 +0000 https://codegenblog.kinsta.cloud/?p=43 Dead Code Removal Sucks ☠️ Dead code is a perennial source of tech debt in large codebases. As unused code and outdated feature flags accumulate, the codebase gets weighed down with countless intricate sets of if-else statements and functions. Engineers tiptoe around them, scared to delete them in case of breaking something else, so the […]

The post Automating Dead Code Removal with AI and Static Analysis appeared first on The Codegen Blog.

]]>
Dead Code Removal Sucks ☠

Dead code is a perennial source of tech debt in large codebases. As unused code and outdated feature flags accumulate, the codebase gets weighed down with countless intricate sets of if-else statements and functions. Engineers tiptoe around them, scared to delete them in case of breaking something else, so the dead code paths just lurk around indefinitely.

Automated Solutions

Meta even wrote a whole internal tool, SCARF, that uses advanced code analysis to safely delete unused code. SCARF creates and traverses code dependency graphs across multiple languages, and across API calls, in order to determine whether a piece of code is truly safe to remove—and what else needs to be removed in conjunction with it. In five years, SCARF auto-deleted over 100M lines of code at Meta.

Unfortunately, SCARF is not open source. But you might be wondering: in the age of AI tools, is SCARF still necessary? Can we just make AI remove dead code for us?

In a smaller codebase, sure. But, in a large codebase with complex cross-functional dependencies, it’s hard to pass in enough context to an AI agent—or to make it conduct a thorough enough code analysis—to trust it to come up with consistently safe deletions.

That’s where a combined static analysis + AI tool can come in. Codegen combines code analysis with AI tools to auto-generate dead code deletion PRs that you can trust.

A small snippet of a dead code analysis that we ran on the Codegen internal codebase. Codegen’s static analysis tool mapped out all function dependencies and marked unused functions in red in this graph.

Let’s look at one very practical example of automated dead code deletion—feature flag deletion.

Feature Flag Deletion

Feature flags are a particularly pernicious form of dead code buildup. As Anton Zaides writes:

  • Each feature flag increases your testing burden by 2x – you need to test the feature with and without the flag.
  • Feature flags take up tons of time even to maintain, let alone delete. Anton recounts seeing a developer spend a whole day refactoring code under a feature flag that was never even released.

Feature flags can inflate your costs, too, especially if you’re using a feature management service like LaunchDarkly that charges for each feature flag or connection.

With Codegen, you can connect your Github repo, type in the name of a feature flag you want to remove, and get an auto-generated diff removing the flag.

Here’s a diff that Codegen produced when we entered the flag name VALIDATE_CARD_ELIGIBILITY_BEFORE_SA_CREATION:

Because Codegen has indexed the entire codebase into a dependency graph, we can guarantee that each feature flag removal is comprehensive and non-breaking.

Though we chose a straightforward example for this blog post, you can imagine much more complex cases where a feature flag spans many files across the codebase.

Want to try this on your own codebase? Request a demo here.

The post Automating Dead Code Removal with AI and Static Analysis appeared first on The Codegen Blog.

]]>
Case Study: AI-Powered SQLAlchemy Upgrade https://codegen.com/blog/case-study-ai-powered-sqlalchemy-upgrade/ Wed, 04 Dec 2024 20:58:52 +0000 https://codegenblog.kinsta.cloud/?p=32 Upgrading your tech stack is like cleaning out your closet. You know it needs to happen, but, ah, it’s such a headache. But what if an AI tool could handle the heavy lifting for you? Today we’ll go through a case study on how Codegen’s AI can safely automate your migration from SQLAlchemy 1.6 to 2.0. […]

The post Case Study: AI-Powered SQLAlchemy Upgrade appeared first on The Codegen Blog.

]]>
Upgrading your tech stack is like cleaning out your closet. You know it needs to happen, but, ah, it’s such a headache.

But what if an AI tool could handle the heavy lifting for you? Today we’ll go through a case study on how Codegen’s AI can safely automate your migration from SQLAlchemy 1.6 to 2.0.

Why Upgrade to 2.0?

SQLAlchemy 2.0 makes your code cleaner, less error-prone, and more maintainable. By upgrading, you get streamlined ORM functionality for query handling and APIs, AsyncIO support, type annotations, removal of implicit aliasing, and more.

But manually upgrading to 2.0 is hard. Numerous APIs and ORM methods have been deprecated or changed. Overlooking even a small detail can cause messy runtime errors.

How Codegen Automates the Migration

  1. With our proprietary static analysis software, we index your entire codebase and build a map of how everything depends on each other.
  2. Our AI refactors your code to use the latest APIs and patterns from SQLAlchemy 2.0. Thanks to the static analysis, we ensure that changes are comprehensive and won’t break either upstream or downstream dependencies.
  3. We run your unit tests and lint rules against the changes to ensure there were no errors or regressions.

Case Study: Codegen’s Automated Migration

This Github repo shows a before-and-after comparison of a Codegen-assisted SQLAlchemy 2.0 migration for a simple book-tracking app. The migration guide breaks it down into a series of Codemods, such as this one that updates all files to adhere to SQLAlchemy2.0 class inheritance protocol:

Fork this codemod on run on any codebase, and it’ll generate a diff updating your SQLAlchemy class inheritance protocols.

Here are some highlighted diffs from the complete migration.

Removing implicit autocommit
By adhering to 2.0 syntax, we reduce the chances of unintended side effects and ensure the connection is properly closed
Updating query syntax to use where() and select() constructs
Instead of query and filter, we use the updated select and where statements.
Using new relationship loading techniques
With lazy=”selectin”, we invoke a SQLAlchemy 2.0 lazy-loading strategy that loads related objects using a single additional query with an IN clause, instead of executing multiple subqueries.

Personally, I’m getting a migraine just thinking about making all these updates by hand, even for a simple CRUD app like this.

Set up a demo to get started with Codegen today!

The post Case Study: AI-Powered SQLAlchemy Upgrade appeared first on The Codegen Blog.

]]>