GitHub Copilot is a great tool. It increases your coding efficiency and can help you make your code better, but it also has some neat hidden features you might not have known about. In this blog post, I’ll share three practical tips for using Copilot. These tips are intended to provide new ideas and insights that can enhance your daily experience with Copilot.
Make Lists
Copilot is a tool that can assist with creating data lists. By providing Copilot with hints and a single example, it can generate the entire list for you. For instance, if you wanted to add ISO country codes to a JSON file, you could name the entity as ‘Countries’, give an example country, and Copilot would populate the rest. It’s important to note that this functionality is not limited to code files, but can be used more broadly like for JSON files.
Write Unit Tests
Copilot is great at writing unit tests and making unit test templates. It uses fairly popular AAA method to do the tests and I think that this structure is great for unit tests. In this example I have a class called BankAccount. I want to write Xunit unit tests against that class, so I have created class called BankAccountTests and added Xunit as using statement. Then I just write a comment where I ask Copilot to create unit tests for BankAccount class and use tab to accept the code suggestions.
This is the test method that Copilot created for me. It added Arrange-Act-Assert comments and made a unit test for Debit method (one of the BankAccount class methods). BankAccount class is copied from Microsoft example, so I think that may have affect how the Copilot is structuring this test. I like how small the test is and how well it is structured.
[Fact] public void Debit_WithValidAmount_UpdatesBalance() { // Arrange double beginningBalance = 11.99; double debitAmount = 4.55; double expected = 7.44; BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance); // Act account.Debit(debitAmount); // Assert double actual = account.Balance; Assert.Equal(expected, actual, 2); }
Whip it for Better Solutions
Sometimes Copilot tries to take the easiest approach and you need to spank it a little bit to get the better results. In this example I asked Copilot to write a bubble sort algorithm for me, but I wasn’t happy about the given solution. Then I asked Copilot to give me the Optimized solution, that is actually little bit faster than the first option.
You can make more accurate comments and naming to improve the code suggestions. You can also use the build-in Visual Studio window that will show a list of possible solutions. Usually you can find the suitable solution directly from that list.
Note: You should always use already implemented sort algorithms and not write your own. This is just for example.
This is the optimized solution and as you can see Copilot added swapped flag that will break the execution if the given array is already in correct order.
for (int i = 0; i < array.Length; i++) { bool swapped = false; for (int j = 0; j < array.Length - 1; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; swapped = true; } } if (!swapped) { break; } }
Summary
Copilot is a powerful tool with numerous hidden features that can come in handy. The best way to familiarize yourself with these features is to simply start using Copilot and experiment with different approaches when faced with a problem. Whether it’s writing templates or code comments, try them out and see if they can help you solve your problem.