Next, let's rewrite the database connection opening to use Gorm. Anecdotally, I had worse results when translating more complex models across packages in a real-life refactor. Then I type out a struct definition that I hope will work, and actually, it looks like it will: ![]() We start by importing the Gorm libraries, so Copilot will consider them available (I hope):Įnter fullscreen mode Exit fullscreen mode Here, let's go ahead and refactor to use Gorm, a lightweight ORM library. Inline database queries aren't always a bad choice, but I like to at least separate my code that accesses the database from the business logic. I would expect to see the tooling evolve to where the whole process could be one command, though, so it's too early to declare AI defeated by a little latency. The rest of the way, I find Copilot takes a little longer to respond so I mostly just do the refactoring by hand. The next time around, Copilot has a better idea of what I want (probably because it notices the increased use of the new method): Okay, we'll give it a hint by typing in the method name on our own: There isn't a way to ask Copilot to go and refactor code for me (although I would not be surprised to see this in the future keep an eye on Copilot Labs), so instead I'll delete the old code and see if Copilot suggests our new function instead. ![]() (That describes my coworkers, too, to be honest.) This is a common theme with Copilot: it can't read my mind (yet?), but sometimes what it comes up with still works. This isn't how I'd have written it, but I can also live with it. I then hit TAB over and over until Copilot has finished the function for me: Hey, actually, that's pretty much what I wanted. Then I hit ENTER and Copilot jumps in with a suggestion for the signature: I hit TAB and save myself some keystrokes. You're not wrong, Copilot you're just unhelpful.īetter. I started by typing out a comment for the function I planned to write, and Copilot jumped in: Now, this is an easy extraction that we don't need AI for, but let's see how it does anyway. The "render an error message" block is repeated many times in this API, even as simple as it is. But most of us are working on codebases where that's not an option, so I will demonstrate a more targeted approach in this article. Quick aside: This snippet is short enough that you could probably pass the whole dang thing into ChatGPT and ask it to do the refactoring. But the code is repetitive, uses a global variable for the database connection, and doesn't have any data layer separation. Here's a little code that we'll refactor with our eventual replacementCopilot's help:Īll it does is create, list, and show simple blog objects in JSON form. ![]() Specifically, let's see how well GitHub Copilot, a generative AI model powered by OpenAI's Codex, does at refactoring some Go code. But even before comforting ourselves by considering all the aspects of our job that an AI couldn't do (like clarifying requirements between multiple stakeholders, managing projects, or handling production issues), let's take a look at how good a job AI can actually do at coding. Could an AI have just done that for you? Especially with a little supervision? Think about how many CRUD APIs you've implemented. But then again: neither is most of the code being written today. Granted, the content they produce is generally not that interesting: I like Ted Chiang's description of ChatGPT as a "Blurry JPEG of the Web". These all blew away my perceptions of what AI is capable of. Programming is hard, after all, and Turing already proved that no computer can even tell if a program will halt or not, so what was there to worry about? Machine learning was neat, but it was mostly good at classification, not generating content.īut then came the latest wave of generative AI: DALL-E, Stable Diffusion, GPT-3, ChatGPT. In the past, I've always laughed off comments like this from non-technical friends and family. Aren't you worried they'll just replace you with computers?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |