A practical example of using AI for coding
An end-to-end walkthrough of designing, developing, and testing a simple application with the help of AI.
ChatGPT and other LLMs have impressive coding capabilities. However, the obvious way to use it is like driving a race car on square wheels — inefficient, teeth-rattling, and headache-inducing.
I've developed some techniques for working with AI coding tools that get good results. Here, I'll focus on how you can add generative AI to your workflow.
I've done this exercise as a live demo before. Showing someone how to do something opens doors to many previously unthought possibilities.
Let me give you the details.
But first, let's discuss the question I'm asked most often when I demo:
Will AI replace software developers?
Everyone has been talking about generative AI for development, but I have yet to see practical examples of its use. Many posts say how AI will replace all of us developers. Many posts show examples of generated code doing simple things -- often incorrectly. A few have come to the same conclusion as I have:
AI won't replace developers; developers that use AI will replace those who don't.
At least in the short term.
Eventually, AI may replace software developers.
In the meantime, I'm embracing it and figuring out how it can make me more efficient.
It's dangerous to go alone!
Treat the AI like a coding companion -- you are pair programming. It has access to a lot of knowledge and can put it to use quickly. You are a reasoning being and have insight that the AI doesn't. Do what you do best and let it do what it does best.
Be specific; be precise. AI is not omniscient. It can't read your mind (*yet). A fun way to ask AI to teach you something complex: "Explain X like I'm 10" or "Explain X in layman's terms." Do the reverse. Explain what you want in exacting detail; otherwise, the AI will make assumptions or make stuff up. Pro tip: Tell it to ask clarifying questions.
You must already know what you're doing, or you won't know if the AI is right. Only do this for languages and libraries you are intimately familiar with. At the very least, you should know the language well and be willing to learn the library instead of just copy-pasting whatever the AI generates. You are the editor. You need to understand what it is generating.
Be fully engaged. The goal is to speed up your development -- not have the AI do all the work.
You might see a common theme with these tips. The experience is very much like pair programming. Consider it like you're the lead on a project pairing with a junior developer. A junior developer that just so happens to know most of the internet before September 2021.
Take this
No one AI tool does everything. I've tried several; for this example, we'll keep it simple.
ChatGPT will do the bulk of the generating. You could do this with GPT 3.5 or 4. I find 4 much better at generating code and understanding context. But GPT 4 is limited to 25 messages every 3 hours and requires ChatGPT Plus. I'll give more tips on how to make the most of those 25 messages later.
CodeWhisperer will handle auto-complete. I'm currently using this over Copilot because I like its PyCharm plugin better.
CodeGPT plugin will let our IDE talk to ChatGPT (but not that CodeGPT). I use the IntelliJ plugin. I have yet to try the unrelated CodeGPT service, which only provides a VSCode plugin. The goal here is to have ChatGPT integrated into the IDE. Some of these don't have GPT 4 access, so we still need to use the standalone ChatGPT if we want to use that.
ChatGPT is not the only tool. Whatever you're trying to do, there's an AI for that. For this example, though, let's stick to the basics (he says about an AI that has transformed the world).
Hold up
We can't just jump straight into coding. We wouldn't be very good project leads if we did.
For the best results, work with the AI in multiple, discrete steps and sessions:
Design a solution through conversation. Effective software development starts with a design. AI is great for bouncing ideas off of and helping you think out loud.
Implement tests based on business cases. I usually write tests during coding -- interwoven with my coding. In this case, I'm generating tests first, then using a different AI to write code to demonstrate the use cases of multiple AIs — also, because test-first development.
Write code in the IDE with AI plugins. We'll do this in an interesting way. I promise.
Fix the code by using the tests. Yes, you will need to fix the code.
There's another reason to break this into multiple steps:
GPT 4 is way better at everything compared to GPT 3.5. But it costs money, is relatively slow, and has a message limit.
If you don't have GPT 4 access, 3.5 will work with more guidance from you.
To get the most out of GPT 4, split the load.
Use GPT 3.5 to help design and GPT 4 to generate code. GPT 3.5 is generally good enough at software design to tease out the details needed for a good design. If you can coach GPT 3.5 to develop a good design, you understand the problem well enough.
Use GPT 4 to coach if you are working on your design skills, and then use GPT 3.5 for code.
Split GPT 4 sessions to be able to use more than 25 messages. Handle design in one session and coding in another.
Ask ChatGPT to generate prompts to use with other AIs or AI sessions. For example, "generate a prompt to tell another AI what we just discussed."
Onward
This is the process we’ll follow:
Consult with ChatGPT to design a solution and generate a lightweight design document. We will use this as input for all the other steps.
ChatGPT will generate a PlantUML diagram from the design specification. This gives us a nice visual to put in our design document and lets us verify ChatGPT understands our design (us too).
ChatGPT will generate an OpenAPI spec from the design specification. We’ll feed this into other design components. Also, I like API-first development.
ChatGPT will generate tests from the design specification. It helps to pair this with the design document because good tests describe the business use cases of your software.
Switch to IDE.
CodeGPT will generate application code from the tests. Test-first development — this is the stuff of science fiction!
Pair with CodeGPT and CodeWhisperer to ensure the tests pass and verify the correct functionality.
Profit.
For the best results, don’t just say, “Write me code that does X”; use a prompt that:
Clearly defines roles for you and the AI: "You and I are software engineers. We will use pair development."
Sets a goal: "Our goal is to craft the best possible application to satisfy the design."
Defines a process to follow: "We will follow this process: {bullet list of process}"
Lets the AI ask questions: "You will ask me questions about assumptions until all your questions are answered."
Prompt frameworks make AI interactions more effective. They ensure clearer, relevant responses and save time. Frameworks maintain consistency, facilitate more straightforward data interpretation, and encourage effective learning. The #prompt-library channel in the OpenAPI Discord is a top resource.
Let’s move on to designing with AI.
The Scenario
We need an example to work with. AI is pretty great at leet code examples. I am not. Also, I said this would be a practical example. Leet code is not indicative of real-life work.
For practical examples, it helps to use something similar to real-life work.
Coding Challenges is an excellent resource for building real-world applications.
A URL Shortener Challenge looks like a good, practical example to work with.
Next
Head to Part 2: Continue the journey.
Part 1: Setup and Process ← YOU ARE HERE
Part 3: Sequence diagram and API specification
Part 4: Generating tests
Part 5: Generating the application and making it work
Looking forward to seeing how it copes with the coding challenge.