GitHub Copilot in flight
GitHub Copilot?
If you haven't heard about GitHub Copilot yet, it is an AI-powered code assistant which can suggest whole lines and entire functions of code inside your favorite IDE.
Copilot in flight
Just when I started to create a simple compiler in TypeScript, I got access to GitHub Copilot. I got excited and instantly integrated it into VSCode and started using it. My first thought was it will only slow me down because I have to interpret and correct every single line of code it suggests. Soon I realized it made the development faster than I could have imagined.
The first thing I tried is to make Copilot suggest me a complete implementation of a CircularBuffer utility class. But it only suggested a simple Buffer class, which implementation was incorrect and I had to fix it manually.
Probably this was a bigger challenge for it, so I decided to only use it for smaller tasks like function implementations and unit test writing. In the beginning, it still suggested some irrelevant and incorrect code, but as the project grew the suggestion got better.
My experience is that it adapts to the developer/project, so you can't trust it 100% (as it is noted on their website). For example, at first, I started writing unit tests like this:
And soon it started suggesting similar code. Imagine a test case where I want to test a more complex expression, like this: 10 * (4 + 16) / 10
. It would be messy, and unreadable. So I refactored the test cases to assert the generated syntax tree.
You can see that it's much easier to read this test case than the previous one. Copilot quickly adapted to this and started suggesting relevant code, to make the development faster.
Another example is when I wanted to parse a simple expression with a single function call for the first time.
At first sight, this code suggestion looks good, but it does not fit entirely well into my project. First, there is no CallNode
, only a FunctionCallNode
(It was defined before I started writing this test case). Also, the node first parameter should be a token and not another node. You can see a possible implementation below.
After this test case was implemented, Copilot started suggesting correct implementations for similar test cases. Below is a nested function call test case suggested by Copilot.
I also tried to observe Copilot during the core components implementation. So, I gave it a task to suggest a function call parser, and to my surprise, it suggested a nice code snippet.
But there is some problem with the above code,
- It should create a new
FunctionCallNode
and not aVarNode
, - If the current token is not a function identifier, it should forward the control and try to parse an expression,
- The parsed function must have one and only one parameter, and it is a big limitation.
As you can see in the above implementation the previously mentioned errors are fixed and the parsed function calls can have zero or more parameters.
Conclusion
For me, Copilot was very helpful for some tedious work, especially when I had to write almost the same code over and over (ex. token definitions, specific node types), and it was also very useful for writing unit tests. For the core components implementation, I mostly used it as an autocomplete to type faster, and not as an assistant who suggests whole implementations.
In my opinion, Copilot adapts to the project/developer, therefore it does not make anyone a better developer, but it can make a good developer faster. We still have to understand the suggested code and correct any mistake it makes, but it looks very promising.
Thank you for reading my first ever article, I hope you enjoyed it.
Top comments (0)