r/webdev • u/hilav19660 • 2h ago
Question AI agent prompts for optimizing and refactoring Laravel codebase?
Probably everyone will hate me for asking this but I want to refactor and optimize the codebase for my laravel project but I am not sure what to ask the AI agent to do. When I ask it to inspect the project, it always finds something and it will never just tell me "yeah that's good, let's leave it at this". I also know this may vary from project to project but if anyone more familiar with laravel can help me what to focus on, I'd appreciate it very much! 😇
0
Upvotes
2
u/Winter_Stuff9034 1h ago
Of course it never says "this is fine, leave it", you asked it to inspect, not to fix a specific thing. Run Larastan at whatever level your project can survive, paste it the actual errors, and tell it to clear those and touch nothing else. Pint --dirty and the slow queries in Telescope give you the same kind of finite list. Without a tool deciding what's broken first, the agent just keeps finding work for itself.
5
u/SPEZ_IS_A_JABRONI 2h ago
make no mistakesÂ
4
u/justjooshing 2h ago
Try reading up on the things you're trying to improve, otherwise how are you going to know whether AI is focusing on the right thing or not?
1
u/electricity_is_life 2h ago
I'm not trying to be funny but, can't you decide that for yourself? Certainly you can adjust your prompt (like asking it to find "easy wins" or "significant flaws" instead of just any problem), but ultimately it's a tool and you're going to have to use your own judgement sometimes.
3
u/creaturefeature16 2h ago
If you have Fable generate a code snippet and then you open up a new instance and ask it to do an adversarial review, it will provide a scathing takedown and the corrected code. If you then take that code and do the same thing in a new chat instance, requesting another adversarial review, it will perform yet another scathing takedown. Repeat ad-infinitum.
The point being is that these models are always going to be finishing the story that you start. A better option is just asking it to audit the code base and look for potential areas for improvement so you can get a running tally and lay of the land...but the decision making has to come from you.
The catch 22 with these models is that they demand expertise to be used effectively...but if you already had that expertise, then they aren't as useful to you, at least for that purpose.
Once you do have that expertise and you feel like you know what has to happen, they are the best fit in this situation, where they become a sort of delegation layer. That you can use them to offload tasks to, and gain the productivity benefits of having a incredibly fast and smart typing assistant.
But if you defer decision making to the model, you'll never truly know what the right answer is (unless you already know the answer in the first place).
2
u/t1nkl 2h ago
"inspect the project" has no finish line, so it never says it's done. give it a fixed list and something measurable instead, and tell it "if you find nothing for an item, say so and move on".
things that are worth it in most laravel apps:
- n+1 queries. add Model::preventLazyLoading(! $this->app->isProduction()); to AppServiceProvider::boot and click through the app locally. it throws whenever a relation gets lazy loaded on models that came from a multi-row query, which is exactly the n+1 case, so you get a concrete list to hand the agent
- slow queries. look at the actual sql the slow pages run (debugbar or telescope) and ask it about those pages only, plus missing indexes on the columns they filter by
- static analysis. composer require --dev larastan/larastan, add a phpstan.neon that includes its extension, start at a low level and raise it. the errors it reports are the to-do list, and when it passes at the level you picked, that item is done
and get tests around anything before it gets refactored, otherwise you can't tell whether "optimized" also changed behaviour.
1
u/eltron 2h ago
We’ll start off by asking what can be improved or should be, and what steps it would take, and why. Verify that with another model/chat and just work with a markdown file plan. Then when your ready to work, break each piece down into its own piece and do them one at a time. Don’t single short prompt this. Tell it to ask you for making tricky cases or trade off so you understand the impact and don’t make risky choices without user input. Rinse and repeat.
1
u/seweso 2h ago
You are using a product that can then buy more of its own product.Â
Why would it ever stop?