4 ms·
I feel like I ran into a example use case yesterday that really illustrates how I work with AI as a dev. It's a simple db entity, with a field called "parent" t
by reverendsteveii 1y ago
I feel like I ran into a example use case yesterday that really illustrates how I work with AI as a dev. It's a simple db entity, with a field called "parent" that is the id of another entity of the same type. Theoretically there can be any number of parents (though practically w the data we're modeling we don't ever expect more than 3 levels). Classic case for recursion, right? So I whip up a quick method that goes something like
public void getEntityWithParents(List<DBEntity> entityList, String id) {
DBEntity entity = dao.getById(id);
entityList.add(entity);
if (entity.getParent() != null) {
getEntityWithParents(entityList, entity.getId());
}
}
Because I'm working from a make it work first then make it efficient perspective I realize that I'm making a lot of DB calls there. So I pop open gemini, copy/paste that algorithm and ask it "How can I replace this method with a single call to the database, in postgresql?" and it gives me
WITH RECURSIVE parent_chain AS (SELECT id, name, parent, 0 AS depth FROM table WHERE id = :id
UNION ALL SELECT i.id, i.name, i.parent, pc.depth + 1 FROM fam.dx_concept i INNER JOIN parent_chain pc ON i.id = pc.parent)
SELECT id, name, parent FROM parent_chain
ORDER BY depth;
That might have been a day or two of research, instead it was 5 minutes to come up with a theory and an hour or so writing tests. Gemini saved the day there. But it wasn't able to determine what needed done (minimizing DB calls), only how to do it, and it wasn't able to verify correctness. That's where we'll fit in in all of this: figuring out what to do and then making sure we actually did it.
- ednite 1y agoThis is a perfect example of how AI can be a powerful partner in development when you already know what you’re trying to solve. Feels like the real sweet spot right now is: humans define the goal and validate the work, AI just helps fill in the middle.
- reverendsteveii 1y agoif they make an AI that lets you format code readably on HackerNews we are all sauteed
- Jensson 1y agoYou can use 2 spaces before and you get a <code> block. AI should be able to do it for you if you ask it to since its good at transforming between formats. public void getEntityWithParents(List<DBEntity> entityList, String id) { DBEntity entity = dao.getById(id); entityList.add(entity); if (entity.getParent() != null) { getEntityWithParents(entityList, entity.getId()); } }