A nice use-case of AI I found for coding: Find and replace on steroids
I'm in my way to find what are the best use cases for using AI in my coding workflow, and one of the bests I found is using AI as a find and replace in steroids.
We're living in the era of the ✨coding agents✨ but I'm struggling to find on them the big usefulness people seems to find. That was until I thought about using them as smart find and replace machines.
I will illustrate this with an example:
Take two codebases, for example two services, one of them is fetching data from the other through a HTTP request (serviceA), while (serviceB) gets some data from a data source. We make the decision to merge this two services into one because we hate ✨microservices✨ so, one of the first things I may do is move all the files related to get data from the data source from serviceB to serviceA.
// File: serviceB/db.ts
export const getUserData = async (userId: number) => {
// whatever
}
// File: serviceA/client.ts
const userData = await fetch("serviceB/userData/42");
Lets imagine I have, maybe dozens of this type of calls between the services, and now I want to translate them to this:
// File: serviceA/db.ts
export const getUserData = async (userId: number) => {
// whatever
}
// File: serviceA/client.ts
const userData = await getUserData(42);
If this were only a couple of methods, I would just fire up my amazing vim skills, but given that I may have to do this in dozens of fetch requests inside serviceA during the migration, and that is not as simple as just replacing the line in most cases (maybe we need to import a new file, group a bunch of queries into one, etc) I thought this to be a very nice problem from a AI.
Why? Because it is very bounded, and I think LLMs like that. So, I open my opencode instance and ask Claude:
Take a look at the file @serviceA/client.ts
and given all the fetch requests inside it,
go take a look at /serviceB and see the implementation.
You have all the files serviceB used to have inside
serviceA, so you could replace all the fetch calls with
each corresponding implementation.
And it gives me a very nice result, maybe I need to fix one or two things, but it is amazing that I have a tool that can find and replace for me in this smart way, knowing which thing depends on what thing, maybe creating one or two generic methods when needed...
Just review it and good to go! Services merged in record time!
Yeah... maybe not the best code quality, but one of the tedious things to do when doing this kind of "refactors" is this task, and the fun part is deciding how to go from here! Fun part for the human, boring part for the machine, perfect match.