This article contains code assistant tips and tricks that have helped me write better code. It is currently a direct copy of the suggestions provided by SomeOddCodeGuy, and I will refine it as I progress.
Rules
Keep context low
- Most AI I've come across tends to lose quality as the context becomes more complex.
- To work around this, it's helpful to start new conversations frequently and to edit existing messages to reuse context.
- For instance, if the AI generates some code and I need to ask a question about it, I can follow up with that question.
- Later, if I have an unrelated second question, I can edit my original question and replace it with a new one. After that, I can regenerate the AI's response.
Tell them another AI (not you) wrote it
- When asking the LLM to review the code, do it in a new chat and tell it that ANOTHER AI wrote the code. Not you, not it, but a separate AI.
- My prompt usually reads something like: "I presented the following requirements to another AI [your requirements here]. Please review the code critically and critique it, refactoring as necessary."
- I've found that LLMs are too nice when I say I wrote it and double down when I say that they wrote it.
Create Workflows
- This isn't just about time savings but mental energy savings. It means creating a workflow that saves the developer as much effort as possible by engaging the developer only at specific moments.
- There may be times when you read this where you think, "Why do this extra step BEFORE looking it over?" AI can produce a response in 2 minutes or less, while a human can take 5-10 minutes to do the review, which is energy spent. It will make you tired. I'd rather burn some AI time to get it right before the dev engages
AI is a junior dev
- Do not rely on AI entirely. Think of AI as a junior developer—would you task a junior developer with a large-scale application and not even review it? Of course not.
- With AI, you have a junior developer trapped in a little box, writing any code you want. Use that junior developer appropriately, and you'll get a lot of benefit.
2 is better than 1
- Important Note: I always use 2 AI. Always.
- If you don't have a local AI, Mistral has le chat for free, and you could use free ChatGPT 3.5.
- If you have high-end subscriptions, like Claude Opus and ChatGPT 4 Turbo, even better.
- I prefer local AI models for various reasons, and the quality of some like WizardLM-2 8x22b are on par with ChatGPT 4, but use what you have available and feel most comfortable with.
- You CAN use just use one, but different models have different training, and may catch things.
Phase 1: Architecture
AI is terrible at architecture, so this is mostly you. You don't have to deep dive down to, say, the inner/helper method level, but at a minimum you want to document the following:
1. About the project
- What is the project about?
- What are the requirements of the project, in a concise format that you can repeat to the AI over and over again whenever you pose a question to it?
2. Scope
- What does "Done" look like?
- This is for your benefit, really. Scope creep is miserable, and you have no one to reign you in as a stakeholder.
- Trust me; my current project should have been done weeks ago, but I won't quit adding features.
3. Mental model
- What classes/modules/packages should exist? Lay the program out in your head.
- What is each responsible for? How does it flow?
4. Basic code architecture
- At a high level, what kind of methods should each have?
- If you have a LoggingService, do you want a "Log(message)" method? If you have a FileManagerService, do you have a "ReadFile(fileName)" or "ReadFile(filePath)" or something else?
During this phase, you can present the answers to #1 and #2 to your AI and ask it for an architectural breakdown but don't just use its answer. This is just to help you overcome mental blocks, give you something to think about, etc.
Write your own architecture. A big reason is that you, above all, need to know this project's structure inside and out. It will be harder for you to keep track of your project if you don't write your own architecture.
Phase 2: The Coding
Below is the workflow I use. I understand that for many people, this will feel like an unnecessary number of steps, but for me, it has resulted in the highest quality that I've found so far and has sped my development up massively, especially when working in a language I'm not intimately familiar with (like Python—I'm a C# dev, lol).
Yes, you can get code from AI far faster than what I'm about to say by simply asking for it and moving on, but the goal for me here is quality, developer understanding of the code, and adherence to the developer's style of coding. I want to write code that is clean, maintainable, and scalable, and other developers at least won't want to set fire to it if they look at it lol
Note: When I send the AI my first coding prompt of a conversation, I almost always include the answer to #1 from Architecture above—the breakdown of requirements for the full project. That context can sometimes help the AI better understand what you're trying to achieve.
Step 1: Pick a feature
- Look over your architecture and pick a feature.
Step 2: Present requirements
- Present the requirements to the first AI (whichever you want to use first; it doesn't matter) and the high-level overview of the classes and primary methods you want.
- I generally formulate a prompt similar to this:
Prompt
"Please write Python code to read from a file and present the contents to the user. I'd like the code within a module called 'file_utilities', with a class 'FileManager' that has a method called 'read_file' that takes in a file name. I'd then like this called from a module called 'display_utilities', which has a method called 'display_contents_of_file'. This prints to the console the contents of that file. Please consider these requirements, give any critiques or criticism, and write out a solution. If you feel another path would be better, please say so."
- In the prompt, also add to write unit tests.
Step 3: Requirement and Response
- Copy the requirements and response.
- Start a new chat.
- Paste both, telling it that you asked another AI to write the solution, and that was the response.
- Ask it to please critique and refactor.
This wasn't written by me, by you, or by anyone with feelings, so no one will be made sad if you rip it to pieces
Step 4: 2nd AI comes to picture
- Copy the requirements and the new response.
- Go to AI #2 (if applicable) and ask it the same question as above.
Step 5: Manual code review
- Take the final response and code review it yourself.
- How does it look? Do you see any obvious flaws? Is there anything you want to change? Rename any helper methods as necessary. Consider whether any of it looks unnecessary, convoluted, redundant, or simply has a code smell.
Step 6: Restart
- Take the code, the requirements, and all of your feedback and start over from step 2, repeating the whole flow if necessary.
While this may seem like it would be exceptionally time-consuming, I can tell you that this workflow has worked amazingly for me in saving both time and energy. I'm usually dead tired at the end of a workday, and I simply don't have the mental energy to write code for another 4 to 5 hours straight. Because of this, I put off personal projects for YEARS. But by doing this, it allows me to get roughly similar quality to my own work when I'm fresh, while pawning off the labor portion of the dev to the AI.
I do the thinking, it does the effort.
I would expect that steps 2, 3 and 4 will take around 5 minutes total. Step 5 will take 10-20 minutes depending on how much code is involved. Another loop will take another 15-25 minutes. So 1 feature will take around 20-60 minutes or so to produce. But the key here is how much mental energy you, as the developer, conserved while still maintaining tight control over the code.
Also, note that this workflow won't work for EVERYTHING. Context limits can make it simply infeasible to engage the AI in some tasks. Say you've got 6 classes that are all working together on a function, and you realize there's an odd bug that you can't figure out where it is in that workflow. More than likely, you won't find an AI capable of handing that amount of context without degraded quality. In those cases, you're on your own.
Anyhow, I know this is lengthy, but I wanted to share this. This workflow has worked amazingly for me, and I intend to continue refining it as time goes on.