Getting the most from GitHub Copilot

AI coding assistants are in vogue right now. How do you get the most from them?

Jamon Holmgren
Red Shift

--

When GitHub Copilot was released, I immediately tried it out. The claims seemed too good to be true.

What I found was the opposite — it was better than advertised. I was so blown away by it that I… didn’t even tweet about it. I thought it was too big of an advantage to give away for free!

I found Copilot to be so impactful that, when we had our 2021 team retreat, I dedicated a full section on my CTO presentation to Copilot. Using Copilot became about as close to “required” as any tool is at Infinite Red. It was that big of a game-changer.

What Copilot does for my coding workflow

I’m a fairly fast coder in normal circumstances; Copilot doubles my speed.

I have mild ADHD and one of my biggest struggles is staying focused. Once I hit “flow state”, I’m unstoppable. I don’t think I’m alone in struggling with this, but even if you don’t, you might still have moments where you sort of “lose the thread” and need prompting.

I’ve found that my ideal coding flow always involves pairing. A good pair keeps me focused, anticipates what was needed next, offers suggestions, and is patient with me. Copilot fills that role now, automatically. I will write some code, hit a certain point where my brain is trying to flip over to some other subject, but that slight pause is enough for Copilot to pop up with a suggestion, thus triggering my focus again.

Copilot’s limitations

A common mistake is to get poor results from Copilot and blame the tool. Copilot doesn’t write your code for you exactly how you need it in most cases. It does its best based on context, clues, and the amount of training data it has access to. And like any tool, you need to learn how to use it to get the most from it.

Let’s talk about ways to get the most from Copilot.

Writing leading comments for context

Context is important, both for a pair programmer and for an AI coding assistant. Start by writing a clear comment that gives, in plain English, a good idea of what a function is for, what arguments it should take, what it should return. If your comment is vague, expect vague results:

A better comment will give Copilot better info:

Don’t be hesitant to go back and tweak your comment until you get the results you want. You can of course hand-edit the code, but if you get good at writing descriptive comments, it’ll do a better job with future code as well.

In this case, adding “Typed function” introduced types to the function arguments.

These comments aren’t necessarily for the human readers of the code, although they’re readable as-is. I always go back and fine-tune them for human readers after I’m done.

Learn the keyboard shortcuts

If you’re using VS Code, you can find a list of shortcuts here.

  • Accept an inline suggestion: Tab
  • Dismiss an inline suggestion: Esc
  • Show next inline suggestion: Option (⌥)+]
  • Show previous inline suggestion: Option (⌥)+[
  • Trigger inline suggestion: Option (⌥)+\
  • Open GitHub Copilot: Ctrl+Return

If you can’t deal with Copilot popping up suggestions automatically, you can turn off autosuggestions and just use Option+\ anytime you want it.

The ones I use the most, other than Tab/Esc, are Option+[ and ] . This lets me cycle through suggestions until I have the most useful one.

Give Copilot structure to work with

Sometimes Copilot will put stuff inline that you really want to extract into a helper function. Remove the generated function entirely, build the helper function first, and Copilot will usually pick up on it and use it going forward.

This goes for other things, such as hard-coded constant values. Add well-named variables right above the generated code and Copilot will integrate them.

Copilot can help with CLI commands

If you’re like me and can’t remember many git commands, Copilot can help with those too.

In an empty tab, write something like this:

It’s not perfect, but it’ll give you enough clues that should get you close!

Copilot is great at repetitive tasks, like writing tests

One of my biggest mind-blown moments on stream was when Copilot wrote accurate tests for a fairly complex feature I was building. Take a look (timestamp 2:23:00):

Copilot can write comments for you, too!

While writing comments can help Copilot with code, writing code can also help Copilot with comments!

If you have existing code and need to document it, Copilot will often have some idea of what it is.

Copilot can explain code to you (via Copilot Labs)

If you install the Copilot Labs extension, you can open it in the side panel, highlight a block of text, and Copilot will do its best to explain it to you in plain English.

I haven’t found this quite as useful since I can read code fluently, but it could be useful if some code is particularly cryptic.

Copilot can help you learn a new coding language

When I was building react-native-colo-loco, I had to write a Gradle script, which is written in Groovy. I know a little Groovy, but not much. So I focused on writing precise, accurate comments, and let Copilot suggest lines of code. I could then lean on my development experience to pick up on patterns and syntax, and go from there.

You can see the results here (I left almost all the Copilot-leading comments in to show what they were).

If Copilot’s code is way off, just keep building

Copilot loves context. The more you build, the more context it has, and the more useful it becomes. When I decided to rebuild a portion of MobX-State-Tree, Copilot was incredibly helpful, because it could examine the rest of the code base. (The refactor never shipped, but it was a great exercise in learning.)

It’s still early days, but I am pretty bullish on GitHub Copilot. If you have other tips, let me know on Twitter!

If you’re interested in how AI/machine learning works, check out my business partner Gant Laborde’s book, Learning Tensorflow.js. It’s honestly one of the best books I’ve read about AI and super approachable for JavaScript developers.

Acknowledgements:

--

--

Co-founder & CTO @infinite_red. Lutheran, husband, dad to 4, React Native Radio podcast host, Twitch streamer, hockey goalie. Talking shop!