How to Improve Both Speed and Code Quality with GitHub Copilot

Copilot recently saved my bacon.  I had a tight three-week deadline and a lot of code to write.  Copilot, the new AI coding assistant from GitHub, allowed me to meet my deadline with enhanced quality and at a rate substantially faster than I normally travel.  It felt like being pulled along a moving walkway at an airport.

But while I highly recommend Copilot, it's still just a tool, and one that works better in some situations than others.  Below is what I learned about it on my recent project organized as five tips to help you learn Copilot fast and increase your speed, quality, and efficiency.

Let's get right to where Copilot fails before covering where it shines.

🐛 Write More Bugs, Faster

I imagine everyone knows copy-paste bugs.  Someone writes a section of code, later someone copies and pastes that section wholesale into another part of the application.  However, some part of that section required a modification for the new context, or maybe the original had a bug in it that was just duplicated.  The result is at least one subtle bug that looks reasonable at first glance.

Copilot elevates copy-paste bugs to a whole new level.  Consider this:

I did this shortly after starting with Copilot.  I was so impressed that after I converted just one hex string to binary in comments Copilot figured out what I was doing for the second and subsequent lines and auto-suggested the binary conversions.

The 2nd and 3rd lines were correct, and the 4th looked right, and all too quickly I developed a false sense of security.  I stopped double-checking.

BIG MISTAKE.  New AI Enhanced Bug (nearly) introduced.

ProTip #1 - Double Check All Output

Copilot is not infallible, and everything it proposes must be double-checked.  Like fertilizer on a garden, it can supercharge weeds as well as vegetables and flowers.  So be careful, and never trust what it auto-suggests.

🪤 The Distraction Trap

I find there are different phases of the software development process that require different mindsets.  For instance, both testing and self-reviewing PR's requires a dispassionate, critical mindset.  Working on new problems requires creativity. And following existing patterns to build out CRUD functionality allows you to turn up the jams, turn down the creativity, and give your frontal cortex a break.  Copilot works amazingly in that latter modality.  

But I've identified two other development modes where Copilot is extremely unhelpful.  The first is when you're establishing a new pattern in your code, such as introducing dependency injection when you didn't start with it from day one.  The second is when you need to solve a hard problem specific to your domain and there are no existing patterns that Copilot could possibly follow.

In both of these cases, Copilot will attempt to throw code up at you as you type.  It's so very tempting to want to look at that code and see if just maybe Copilot has some magic that can short circuit you having to think.

IT'S A TRAP!  That flash of code leads to distraction and losing the thread of thought right when you need thought continuity the most.

ProTip #2 - Turn it Off

When you know you're doing something unique, turn off Copilot or just hit the escape key as fast as you can to force yourself to do the hard thinking that only an actively engaged, fully focused human mind can ever accomplish.

🤘 Rocking Boilerplate

In my experience software development requires quite a bit of boilerplate coding. Copilot rocks boilerplate coding.  When you're just repeating a pattern, Copilot will accelerate you up to dizzying speeds.  Just remember that speed doesn't always happen on day one.  It takes a few days until Copilot learns your codebase and coding style.

Once you're up to speed though take the extra time it gives you back to fit in some refactoring time to implement the Scout rule and continuously leave the codebase cleaner than you found it.

ProTip #3 - Give It Time

Give Copilot a few days to warm up.  Then enjoy superspeed, but use some of the time saved to improve, not just expand, your codebase.

🏆 Get Specific Solutions, Fast

At some point in this three-week sprint, I spent hours trying to convert a UTC date into the local time zone in Python.  Exasperated and getting nowhere fast, I eventually recalled that Copilot would read the name of a method and auto-suggest an implementation.  Maybe it could kind of search through all of GitHub for me and find what I needed?  I wrote this:

def convert_utc_to_local(utc_dt: datetime) -> datetime:

And as if by magic Copilot instantly found the alternate universe in which there existed an answer:

It worked beautifully and might have saved me hours if I'd only thought to do that up front.

ProTip #4 - Write Small Well Named Functions

If you have a very specific problem to solve that has undoubtedly been solved before, write up a small well-named function (which hopefully you're doing already anyway) and delegate that job to Copilot, and save yourself hours of frustration.

It also works great to give hints to Copilot with inline or block comments.

➿ Improve Consistency

Occasionally I discovered that Copilot would provide a suggestion that doesn't match what I was expecting.  It's tempting to accept that answer if it's good enough, but I also found it extremely helpful to ask yourself why.

Asking why Copilot suggests code different than expected often produces the answer that you aren't consistently following a pattern in your codebase.  That's because Copilot looks at both what you just typed and also at all of the code written thus far in the solution, and if your new code deviates from the prior pattern the autosuggestion will also deviate.

Sometimes that's ok but being alert to it can actually help you improve consistency within your codebase.

ProTip #5 - Use Copilot as a Heuristic

If Copilot suggests something you don't expect, use that as a heuristic to improve consistency.

Conclusion

In my mind Copilot is worth every penny.  It allows developers to write faster boilerplate, find solutions to specific problems faster, and be more consistent.  It comes with drawbacks too, but if you know the drawbacks and prepare for them, then overall it will boost your productivity to new heights.  I would be shocked if AI-assisted coding isn't the expected mode of working within the next few years.  Why not beat the curve and start mastering this impressive tool today?  

So what do you think of AI-enhanced coding?  Please share in the comments your thoughts along with any tips and tricks you've discovered.