Coaching AI to write your code
Working with AI is here and it's skill. Just like google-fu became important in the mid 2000's. A few things I've learned.
I started properly integrating AI into my coding workflow a few weeks ago after I got in trouble for "wasting thousands of dollars of company time" by manually doing things computers can do. 😅
It's not perfect yet but it's pretty good! On net I think it saves time, or at least effort. But yesterday I spent an hour debugging when my editor hallucinated the wrong URL for a config. ¯\_(ツ)_/¯
What AI is good for
Like I've said before AI won't take your job, it's going to make your job easier. You can move up the value chain while computers do more of the grunt work.
Current AI is pretty good at writing atoms or S-programs – code with a tightly scoped spec where you can quickly validate the solution. It cannot engineer a solution to a business problem.
LLMs are fantastic at "I have inputs A, B, C and need them translated to outputs 1, 2, 3" problems however. This is awesome!
Based on what I've seen, I doubt large language models will ever get to a place where they can sit down with a stakeholder, figure out what they need, and engineer solutions to business problems. And that's fine.
Defining the inputs, outputs, and constraints – contracts – of your system is the fun part. If AI can write the code, sweet.
Two ways I use AI
These days I use AI in two primary ways depending on context:
- Cursor is my editor at work. It's a fork of VSCode with AI (Claude I think) integrated into the editor. It kinda understands the whole project and can pull relevant files into context
- ChatGPT Canvas when I need to develop one-off scripts that solve a problem and I don't care to know how
Coaching ChatGPT
Using ChatGPT feels like coaching a less experienced developer. It won't read your mind or magically write good code the first time.
But if you take the code, run your validation, and iterate with feedback, you'll get a solution to your problem. The code will be strictly okay. It won't blow you away with elegance but it works.
And that's all you need when you are, for example, writing a script to count words in your book, or a script to collect wordcount stats from git history, or even a script to package your book for feedback.
That last one was fun because ChatGPT made a dumb choice – parsing markdown with regex – and I asked it to use a library. Then we couldn't get that to work and eventually we reverted back to regex. You can see the full chat here.
The whole thing felt like a collaborative pair programming session. I could keep high level context, steer direction, and validate the code, while AI fiddled with the details.
Mentally this felt easier than jumping up and down levels of abstraction. Not sure it saved time.
Advanced auto-complete with Cursor
No screenshots because they're full of work code. Sorry.
Cursor has saved me time with boilerplate code. It's really good at figuring out "Oh you're spelunking this variable through 5 layers of indirection, lemme help" or "You recently looked at code about Foodazzlers, are you trying to add a GenerateTheFoodazler function here?".
This has been fantastic. It's super nice when you establish clear patterns in your project and your editor can follow them for you.
- Add new prop
- Yes thank you editor, add this same thing in 5 other places
👌 chef's kiss
Why do I have so much boilerplate? We're working on it okay [name|].
Another nice thing I've tried with cursor is to highlight a method, open the chat, and say "Hey add so and so to this function". And it does.
Again this works great when the problem is tightly scoped and easy for you to verify. It won't work if you don't know what you're doing or can't quickly validate the result.
Generating tests
It can even generate tests!
Yes I've said in the past that you shouldn't use AI to generate you tests because tests should describe your code, not derive from your code. This is an important distinction.
Here's where I think it's okay: You have a basic function and need Beyonce Rule testing. You know the code works because you've manually tested it, but you need to put a ring on it and make sure it keeps working in different situations.
Cursor is great at taking a function and saying "Ah looks like you need these 4 different cases and to return blah". Then it generates an integration test and you can run to make sure it works. Great.
Again because you're using it on a tightly scoped function with clear inputs and outputs :)
tl;dr
AI can help you write the code you used to copypaste from StackOverlow. Tightly scoped with a clear input/output.
You have to do the engineering part.
Think iterative collaboration, not one-shot do my work requests.
Cheers,
~Swizec