• OMG AI
  • Posts
  • The Machines Now Help Us Write Code

The Machines Now Help Us Write Code

Pair Programming with AI - Exploring Github CoPilot with NeoVim

The machines, AI Models connected to our programming tools in this case, will help us write code. Not will write the code for us, or take our jobs.

IT WILL HELP US.

I think that will put you in the right mindset for where we're at right now in the AI landscape when it comes to AI and creating computer programs.

But first a lovely diversion – a love letter to creation – to programming.

Humans Writing Programs

There are a few things I like to do more than program. It's a pure act. One of creativity and deep thinking. Sometimes I think I must like it too much, because there are a lot of starts and stops, thinking, typing, deleting, and re-typing the same thing. Debugging! Oh, the debugging. And despite all of that, I keep coming back to it.

You travel through heaven and hell a few times a day as a full-time software engineer. One minute you're creating worlds, the next minute you're thinking that a career in woodworking sounds fantastic.

I'm a proponent of pair programming. I think you can learn a lot from your fellow humans. But it's a delicate dance. You both have to be in the right mindset, and you can't let it drag on, you have to have a goal. It's taking a journey and you have to let your ego go, and get the job done in front of you – together.

But what if the person you were pair programming with was just sending you chat messages each time you got stuck, describing what the next line of code might need to look like? You can choose to accept, or refuse their suggestion of what that next line of code is. Sometimes it's wrong, and sometimes it's subtly wrong, which can be worse. But other times you can fly through a straightforward function that may have taken you 10 - 20% more time to write without this helpful chat companion.

You know that feeling right? As a programmer you're either cooking, and slinging code like a cowboy, or you're stuck, and you have to not give up.

This meme captures it perfectly.

Pair Programming with AI

Back to the AI helping us. How will it help us programmers today? You can connect it to JetBrains, Visual Studio Code, NeoVim, etc. It can help with the nitty-gritty, the things we get stuck on, but are straight forward. The big-sky creating worlds via code is still up to us.

Using Github Copilot in our IDE will give us that AI companion. And listen, it's not perfect. It can steer you in the wrong direction, but so can't other humans? Wouldn't it be nice to get "unstuck" more often? You have to take everything it "suggests" with a grain of salt, but most of the time (and it's always getting better), it does a good job. It HELPS.Let's look at how you can get started with it first.

Checking Out NeoVim with Copilot

The onboarding experience is pretty straightforward once you signup for GitHub Copilot ($10/month for single users at the time of this writing), then you're taken to a page to choose which IDE you need to integrate it with.

Clicking on NeoVim shows the setup instructions which is simple as the following:

1. Cloning their NeoVim plugin into your config folder

git clone https://github.com/github/copilot.vim ~/.config/nvim/pack/github/start/copilot.vim

2. Running ":Copilot setup" in NeoVim, which takes you through opening a webpage to authenticate with Github and enter the auto-generated number that the plugin created for you.

3. And running ":Copilot enable", then at anytime you can run ":Copilot status" to see what the status of the service and plugin are, and ":Copilot help" to see their help page in NeoVim

Note: To use GitHub Copilot in Neovim you must have Neovim version 0.6 or above and Node.js version 17 or below installed. For more information, see the Neovim documentation and the Node.js website.

Now as you type, and hit Copilot will give you suggestions for code completion in NeoVim.

Here's what it looks like on my personal website, which is written in Next.js.

As you can see as I hit enter, it autocompletes what it thinks the next line should be and it looked at the surrounding code and did a pretty good job of starting to build out the component that would display all posts in the this page. Pretty awesome!

Right now think of this as autocomplete on steroids. It's got a lot of training behind it to predict what the next word or line should be in your code. It's trained on billions of lines of public code. It's trained using Open AI's Codex model – so it's a version of GTP-3 tuned for converting natural language commands into code. This is obviously why Github Copilot works very well when you put a code comment first, it tries to accomplish the thing you told it to do in the comment. This is probably the most straightforward use case for using it, but here's a list of other ideas you can try that are neat:

  • Helping you write tests! There is a lot of boilerplate in most kinds of test writing, let GitHub Copilot write that boilerplate for you, and you can concentrate on the interesting/important bits of the tests. Expand the edges of the tests, ask it to look for new edge-cases in the current test. A lot of things here I think that can drive quality for an engineer using GitHub Copilot.

  • Generate a regex for you based on text you define

  • Assist non-native English speakers (Works across languages, can translate back and forth!)

  • Preparing for technical interviews. Try to solve the problem yourself without Copilot on, then if you get stuck turn it on, and have it help you get unstuck. Ask it about runtime or specify a runtime if you're doing an algorithm problem, and get specific. Take notes of how you got stuck, what you didn't understand.

  • Translate one codebase into another. If you've ever been in a situation where you're migrating from one language to another, or one library to another, Copilot is useful here too. Give it a code snippet in Go, and ask it to re-write it in idiomatic Python.

There are many more creative ways to use GitHub Copilot. To find more, or contribute more if you don't see it on the list, checkout Awesome Github GoPilot.

Programs Writing Programs

We're early in this AI-assisted software writing journey. It's clunky, imperfect, and their are legal issues we've not cleared in terms of how we train these models even.

We have a long way to go before we get all sci-fi (programs writing other programs, a la Matrix). That being said things are moving fast. Faster than I think we may be comfortable with, and this is why I started this newsletter. I don't want to be in a place where I don't understand the issues with AI, my career, and how software is built.

Tweet of the Week

Let's keep learning.

Kev