This document relays the given demo and the specific tasks that are shown with an example of the query we can give to GitHub Copilot.
This demo takes a rather simple algorithm where we need to clean up the code and unit-test the provided algorithm. As an extra we will end the demo by optimizing the algorithm.
- Step 0: The features
- Step 1: Explain the present code
- Step 2: Rename seed variable
- Step 3: Rename class name
- Step 4: Style refactoring
- Step 5: Generating tests
- Step 6: Algorithm optimization
- Code generation (disabled during demo)
- GitHub Copilot menu
- New chat
- Chat history
- Clean up chats
- Reference adding
- Chatting and conversing
First step is getting a plain explanation on the code that is presented.
Open the GitHub Copilot Chat window and start a new chat.
By selecting the plus
button, add the class Algorithm
.
Then type: /explain
The second step is the first cleanup action. We want a better name for the seed variable.
First, start a new chat with Copilot by selecting the plus
icon in the upper right corner
of the chat window.
Pres the plus button underneath the chat box and select the class we are going to work on.
Now ask Copilot the following:
I want a better name for the 'seed' variable. What would be a better suitable name that also easily shows its function within the class. It should contain the word 'value'
If you like the suggestion, refactor the code. If not, ask again but specify what you would like or didn't like.
In the same chat, let's get a better class name as well. Class names should explain the main function that the class holds.
With the Algorithm class still selected in the chat,
ask the following:
I want to change the name of the class to a more descriptive name that explains it's main function.
Now again, change the code.
Now, let's get the code refactored a bit faster. In the same chat, with the Algorithm class still selected, press the plus button of the chat box. And add the StyleAndReferenceDecisions file. This file contains a couple of guidelines for our code.
Now ask:
/simplify
This will suggest a lot of simplifications in the code and small name refactors.
But we want more, so first open a new chat window, add the Algorithm and Style Reference document and ask:
refactor the code and use the reference documentation to make sure our code adheres to our internal guidelines and decisions.
Refactor the code to replicate the generation.
Now go to the Algorithm class and select/highlight the entire constructor.
Within the chat ask:
I want the selected code to have a better exception message and I want to optimize the check for this exception. Keep in mind that the initialValue can not be 0 or 1. Negative numbers will be converted to a positive number.
Refactor the code if you are happy and go to the next step.
Now that we have refactored the code and made it more clear what it does.
Let's add some tests.
Create a new chat, select Algorithm and StyleAndReferenceDecisions documents.
Then type:
Keeping in mind the reference guide, provide the tests we need to test the functionality and all meaningfull boundaries of the class
Check if all boundaries are tested and add meaningfull extra boundaries.
Are there other boundaries we should test?
As a last part, we will be refactoring the algorithm to optimize it for larger numbers. This will be a conversation, so:
- Ask for possible algorithmes
- Request what would be best suited for speed with larger numbers
- Ask help with the refactoring
- RUN THE TESTS
- Ask Copilot to help if a test fails, but let's hope they don't
That's it. Awesome tool, right?