I decided to make my next hobby project in python. I used this project to learn python, but also to become familiar with AI-assisted coding. In this article I document my experiences with AI as a pair programming buddy, doing Test-Driven Development together.
About TDD, the project and me
Test-Driven Development is about writing tests before writing your code. This is often done in the Red, Green, Refactor cycle: Write a failing test, specifying the behaviour that the code should exhibit, make all tests pass with as few changes as possible and finally make your code (and your tests) nice. It's a design tool that helps make your code easier to understand and maintain.
The project is a self-hosted information database that connects Animal Crossing Amiibos to information on Nookipedia. An API exposes the data so that an app can scan an amiibo and direct the user straight to the character's page on the Nookipedia. The project, besides providing desired functionality, is also an excuse for me to try out some enterprise software patterns that I wish to learn more about.
D-Inventor
/
my-animal-crossing-app
This is a hobby project. It is a combination of an app for android and a python-based server API. The server supplies information about Animal Crossing villagers. The app lets me scan Amiibo cards and then I can see basic information about the villager.
This is a hobby project. I wanted to get easy access to basic villager information from my Amiibo card collection.
This project combines an android app with a python-based API so that I can scan the NFC chip of an amiibo card and then fetch the information. That's all.
I am a dotnet developer with very little python experience. Python has a special place in my heart, because it's the first language that I earned some money with. That was more than 10 years ago and I haven't touched python that much since then.
AI as a pair programming buddy
Most of the code in this project has been written by AI. Overall, the results were mostly good, sometimes very frustrating, but sometimes also very impressive.
Scaffolding a project baseline
Yes, AI got me started very quickly and effectively. I knew what I wanted: multiple python projects in a monorepo with some shared modules. I know what python code looks like, but actually setting up a project like this is quite a lot. Setting up testing with pytest, multiple independent modules with a shared pyproject.toml. Module imports are very confusing in python and if you don't know what you're doing, they're difficult to get right sometimes. I asked the AI to do it for me with working examples of unit tests and shared modules. It did all of it, the code ran, the tests worked and the code looked acceptable. Big win 💪
So many comments, so much "documentation"
It was really my own fault and it was quickly fixed. The AI was writing excessive comments. Every generated function filled with comments and "documentation", detailing exactly what the code does. That's not what I need comments for. I can read the code to see what it does. The thing here is that I let the AI generate its own instruction file and the instruction file literally instructed the AI to do this. I simply updated the instruction file and instructed the AI to never write comments.
Stop! Wait! Not so fast! Not so much!
Some people may find it wonderful: just tell the AI what you want to make and it'll happily build the whole thing for you. Or try to at least... Although it's magnificent, I think it's also incredibly irresponsible to let AI generate large amounts of code at once. Especially in a professional environment, I would be held personally responsible for bugs and mistakes and saying "it's the fault of the AI" simply will not fly. It's essential that I can review the code that the AI generates and I can only do this if I develop in small steps.
Test-Driven Development has been very helpful to put a leash on the AI. AI is very eager to write code and will simply sprint to the finish line if you let it. By strictly following the Red, Green, Refactor cycle, I was able to stay more in control of what the AI does and how it does it. It took a bit of trial-and-error, but the following instructions seemed to be very helpful:
- Follow strict TDD practice with Red, Green and Refactor phases
- Always run the test in the Red phase to verify that the test fails in the way that we expect
- Prefer runtime failures over compile-time failures at all times
- Prefer failures on assertions over failures in code under test whenever this is reasonable
- Make sure in the green phase to write as little code as possible to make the test pass
- Always ask for verification from the user after each phase before proceeding to the next
Additionally, it seemed helpful when I start each session with "We are pair programming buddies and we are going to do Test Driven Development". Nevertheless, the AI would still occasionally do more than I asked for, especially the green phase was difficult at times.
Please give me an example in the form of an automated test
In this project I needed a handful of slightly more advanced concepts, including:
- Messaging
- HTTP API calls
- Database querying
Though the concepts are familiar, I had never done them in python specifically. I know approximately what good integrations look like, but I simply needed some examples in python specifically. AI has been an excellent tool for learning and it helped me to understand python frameworks, tools and syntax. By asking about it in the shape of an automated test, I could also immediately apply the concepts I learned with Test Driven Development. This was a massive win! I would like to share a particular experience that really blew my mind.
I needed to build an integration with an external HTTP API. Coincidentally, I recently also had to do this for work in dotnet, so I had a good idea what a good integration looks like and also how a good unit test looks for such an integration. It had taken me about 5 hours to build in dotnet. I asked the AI to generate me an example of a unit test that demonstrates an http request and response in python. the unit test was quite bloated and unreadable, but it did work. I told the AI as much and I gave it an example of what I expected the test to look like, based on a test that I wrote in dotnet. I asked to refactor the test to make it as close as possible to the example that I gave. In just a few minutes it generated exactly the testing utility that I had spent several hours on earlier! This was shocking and incredibly impressive.
Thoughts overall
In general, I find the workflow decent. Although I did get much further with AI in a shorter amount of time than I would have without, I don't believe (yet) that AI is a massive time-saver overall. That sounds like a contradiction, but you must see that it was my lack of understanding of the python ecosystem that held me back and not the speed by which I type code. Although the AI can create code much faster than me, the code is often not up to standards and at times I have had to throw away work of the past 10 minutes, because prompting for corrections was only making things worse.
An important thing, in my opinion, is that AI may replace your typing, but shouldn't replace your brain. The skills that I've developed as a professional developer are still required to judge the quality of the output and are still essential to prioritize long-term success over short-term gains.
So yeah, if you haven't tried it yet, perhaps you could also turn your AI into your TDD pair programming buddy. Thank you for reading and maybe I'll see you in my next blog! 😊
Top comments (0)