How to use GitHub Copilot to write clean code?
What is GitHub Copilot?
GitHub Copilot is a generative AI that primarily assists developers in writing code by generating code. It is especially effective to write boilerplate code (code that repeats often or that appears frequently in the codebase).
Good examples of boilerplate code are the body of classes following common standards (e.g., Java main class), well-known algorithms (e.g., Fibonacci sequence), and test scenarios.
How does Copilot suggest code?
I use Copilot in IntelliJ as a plugin, and its use is pretty upfront: while you're writing code, it gives a suggestion. Press the Tab key to accept the suggestion, and the code is assimilated. The suggestion is grey in IntelliJ (with the GitHub Copilot plugin).
The more you use it and the bigger the codebase, the more accurate the suggestions become for you.
Why is this helpful?
The answer might seem straightforward: I can use Copilot's suggestion instead of writing the code; that's faster, and I'm more productive. Although true, that differs from where Copilot shines: It feels like pair programming.
Code copilots, such as GitHub Copilot, provide real-time feedback on the code written tendency for the developer to write meaningful code.
For example, when choosing a variable or function name, I can use a name that gives some context on what the function does, like generateSequence, which would be ok. However, if my function name had enough context on what it does, the code suggestion would be way more accurate.
I want to write a function that generates an infinite fibonacci sequence in Kotlin. What is the code generated if I type only the function name for the following?
generateSequence
fun generateSequence(): Sequence<Long> = sequence {
var currentDelay = 0L
while (true) {
yield(currentDelay)
currentDelay *= 2
}
}
generateInfiniteFibonacciSequence
fun generateInfiniteFibonacciSequence(): Sequence<Long> = sequence {
var current = 0L
var next = 1L
while (true) {
yield(current)
val tmp = current + next
current = next
next = tmp
}
}
GitHub Copilot entirely generated both function bodies. The first implementation was different from what I wanted. The second one is exactly what I wanted. The latter has two more (meaningful) words in the name.
I didn't need to search Google or write a prompt asking for such implementation… All I did was write a meaningful function name. That is where one of Copilot's most significant values lies.
If the code written is good enough for an AI to understand the intention, it is more than enough for the developers reading that code to understand. 😉
Clean code
When it comes to clean code, the goal is to write code in a way that any other developer can understand (even yourself long after writing the code), maintain, and collaborate with.
Although there are no clear rules to achieve it, as long the above is followed, you're in a good direction.
I was digging the web to find the most common principles/guidelines to write clean code. I discovered that there's consensus that the following is essential in Clean code:
- Readability
- Testability
Naturally, more is needed to have the cleanest code. However, I want to focus on them because GitHub Copilot excels in these.
Readability
Readability means that your code is easily read (and therefore understood). Writing readable code comes with proper naming for variables, functions, and classes while having clear and straightforward responsibilities for them.
In the Fibonacci sequence example, using generateInfiniteFibonacciSequence instead of generateSequence is more readable because it clarifies the function's scope. The name generateSequence is too broad, as it doesn't describe the sequence type it will generate.
Testability
Writing tests is an essential part of building clean code. They support maintainability and collaboration. Have you ever updated code that has few or no tests? It's a nightmare 😰
The good news is that GitHub Copilot brings the same benefits for tests as it has for readability by providing real-time feedback on how meaningful the code is. The even better news is that it will also help to write tests quickly.
It is challenging to justify delivering test-less code, and a common reason is that there needs to be more time to write unit/integration tests. Well… GitHub Copilot will assist with that.
It generates test code with reasonable accuracy, given you follow a standard in your tests (standardization — yet another benefit for the cleanness of the code). It generated a similar test to the previous one with a different scenario.
Conclusion
GitHub Copilot doesn't only help with writing code faster; it can also help with writing clean code, given the developer writes code that influences the AI in the right direction.
The generated code will not be clean by itself. Still, the AI will provide real-time feedback on how meaningful the code and tests are, assisting with the code readability and testability, which are essential traits of clean code.
Such AIs are excellent tools that require talented people to point them in the right direction. Otherwise, they will only be able to achieve a little.
Do you think you have what it takes to be one of us?
At WAES, we are always looking for the best developers and data engineers to help Dutch companies succeed. If you are interested in becoming a part of our team and moving to The Netherlands, look at our open positions here.
WAES publication
Our content creators constantly create new articles about software development, lifestyle, and WAES. So make sure to follow us on Medium to learn more.
Also, make sure to follow us on our social media:
LinkedIn — Instagram — Twitter — YouTube