{"slug": "large-scale-code-refactoring-with-ai", "title": "Large-Scale Code Refactoring with AI", "summary": "Jovian's founder used Anthropic's Claude Opus 5 to refactor the Jovian codebase from a Next.js 11 frontend and Flask backend to a full-stack TanStack Start app, moving over 5,700 lines of code and updating hundreds of imports in about 20 minutes at a cost of roughly $25. The AI model completed the large-scale refactoring task that would have taken days, with minimal human intervention, and the application worked perfectly afterward.", "body_md": "I'm currently revamping [Jovian](https://jovian.com), migrating it from a [Next.js](https://nextjs.org/) 11 frontend + [Flask](https://flask.palletsprojects.com/en/stable/) (Python) backend to a full-stack [TanStack Start](https://tanstack.com/start) app, while also modernizing the user interface and changing some features. The revamped app is deployed at [https://beta.jovian.com](https://beta.jovian.com).\n\nI started out with a fairly flat structure where the `src/routes`\n\nfolder contained all the [routes](https://tanstack.com/router/latest/docs/routing/file-based-routing) and `src/lib`\n\ncontained files like `utils.ts`\n\n, `utils.server.ts`\n\n, `types.ts`\n\n, `config.ts`\n\n(for environment variables & constants), and `src/db`\n\ncontained the database schema & queries.\n\nThe folder `src/components`\n\ncontained common components used throughout the app, and I placed components & server functions used in specific routes inside folders `-components`\n\nand `-functions`\n\nnext to the route files (the `-`\n\nprefixed folders are [not treated](https://tanstack.com/router/latest/docs/routing/routing-concepts#excluding-files-and-folders-from-routes) as routes).\n\nAs the codebase grew to 15k+ lines of TypeScript, the files in `src/lib`\n\nand `src/db`\n\ngrew to hundreds of lines, some even over a thousand, so I decided to refactor it into a more modular structure, colocating helper functions and constants next to the route files that use them.\n\nA refactor of this scale would have taken at least a few days, if not a week, just a few months ago. However, Claude Opus 5 essentially one-shotted the whole refactor in ~20 minutes and cost around $25. Over 5700 lines of code were moved & hundreds of imports were updated.\n\nHere's the full prompt I used:\n\nJust as we have -components and -functions folders, let's also have a -lib folder which will have utils.ts, utils.server.ts, types.ts etc.\n\nNow I want to keep any environment-derived variables in the top level config.ts and config.server.ts, but the other stuff can go into module-specific -lib/constants.ts and -lib/constants.server.ts files (let's not call it config anymore at the module level).\n\nBasically, I want the entire codebase to now become more modular. And I feel the @src/db/read.server.ts and other db query files are also getting too long. So, let's maybe skip this CRUD-based file pattern and instead have a module-level -lib/db.server.ts files which will have the database queries.\n\nAnd let's move @src/db/schema.server.ts to src/lib/schema.server.ts.\n\nFinally, also move @src/lib/certificate.server.ts and @src/lib/markdown.server.ts to -lib/utils.server.ts files in the right place. I think these files are kinda random and don't fit.\n\nFurther, to better organize the routes by modules of work, let's create route groups e.g. (manage) can be a route group for management etc. in places where the existing directory structure isn't doing sufficient encapsulation.\n\nAnd while we're at it, let's also re-look at the file-based routing setup and see if we made the right choices (e.g. directory vs flat) to organize routes properly (of course, don't change any actual route paths).\n\nAnd let's of course also update the CLAUDE.md according to this new set of guidelines, but don't get too overly verbose (samajdhar to ishara kaafi hai).\n\nAnd finally, I think some of the code comments are way too long, so let's shorten the big walls of text and just keep 2 lines max (unless absolutely necessary) and also establish that guideline.\n\nOkay, that's all I'm thinking right now, tell me if I'm making any big errors or overlooking something important, and if this is the right approach. Let's go.\n\nIt did ask me for a few clarifications (presented as multiple-choice questions) before starting the refactor, and I changed my mind about organizing types midway through the refactor:\n\nOther than that, there was basically no intervention on my part (I did give it a couple of minor follow-up tasks), and the application worked perfectly after the refactor. Without AI, I might have never pursued this refactor. Programming has changed forever, there is no fighting it.", "url": "https://wpnews.pro/news/large-scale-code-refactoring-with-ai", "canonical_source": "https://swiftace.org/posts/large-scale-code-refactoring-with-ai", "published_at": "2026-08-09 17:46:52+00:00", "updated_at": "2026-08-09 18:04:43.852995+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools"], "entities": ["Jovian", "Anthropic", "Claude Opus 5", "Next.js", "Flask", "TanStack Start"], "alternates": {"html": "https://wpnews.pro/news/large-scale-code-refactoring-with-ai", "markdown": "https://wpnews.pro/news/large-scale-code-refactoring-with-ai.md", "text": "https://wpnews.pro/news/large-scale-code-refactoring-with-ai.txt", "jsonld": "https://wpnews.pro/news/large-scale-code-refactoring-with-ai.jsonld"}}