This blog delves into practical tips and best practices for leveraging Copilot’s code completion features to their fullest potential. By following these guidelines, you can enhance your productivity, and streamline your development process in Business Central.
Table of contents
Leverage Context Awareness
Ensure your project structure and files are well-organized. Copilot uses the context from your current file and open files to provide accurate suggestions. The more context it has, the better the completions will be. Keep in mind that context is limited and due that you should:
- Have less files open (3-5 files)
- Files should be not too long, otherwise it gets too much context for Copilot
- Naming is important and it gives additional context to Copilot
Example: Added “Tracking Number” to “Sales Header” table:
Now I want to do the same for the Posted Sales Invoice Header:
As you can see, due SalesHeader.TableExt.al being opened it gives Copilot enough context so it can suggest you same field to be added with same ID, so TransferFields
in posting routing will work!
At this point, Copilot has enough context, so what is next step, usually you add same field to Posted Credit Memo Header. What is now enough for copilot?
I will just write now “tableextension”:
and after just one word all is suggested:
… just one Tab and:
Done, easy as that!
Comment your code
Add comments to explain the purpose of complex code blocks or functions. This not only helps human readers but also assists Copilot in understanding the intent behind your code, leading to more accurate completions.
Example: Let’s create a page for Sales Invoice Header, or better said Page Extension for Posted Sales Invoice:
Comment are there to give additional context, URL format, and Copilot can start working on it:
Here you would see suggestions which are not good. For this part of completion you should use Copilot prompt, by pressing Ctrl + I:
… and, that’s it! Copilot has been able to complete the task based on your context given trough comments in the code.
Code is not by standards by means, message is text and not label. Accept the solution, select the action code and again press Crtl + I:
Let’s do again optimization for URL:
Amazing, right?
And if you want it on Posted Sales Credit Memo, leave PostedSalesInvoice.PageExt.al file open and create new one for posted Sales Credit Memo Page Extension:
Just after typing “pageextension” it suggests the whole code like you did for Posted Sales Invoice, in just seconds. Code is generally perfect except it did one little mistake and that’s page name for Page Extension:
“Posted Sales Cr. Memo” should be: “Posted Sales Credit Memo”
Why is that?
We didn’t have enough context for that one, new project, no variable is declaring page Posted Sales Credit Memo, but it gives it’s best and tries with the file name given Posted Sales Cr. Memo, so it thinks that’s correct. This gives you an idea how important is naming of files, fields, procedures, variables and so on.
Use Intuitive Naming Conventions
Use clear and descriptive names for variables, functions, and objects. This helps Copilot understand your code better and offer more relevant suggestions. For example, instead of using generic names like var1
, use names like CustomerDiscount
or SalesHeader
.
In our example if we did name our action like this:
You see, the result is not what we wanted, as until now Copilot cannot read our minds. 🤣 Even though, in some cases when I was coding it performed like I was thinking in my mind, which got me really surprised!
So, name the action properly:
Immediately you get expected result.
Provide Clear Prompts
When writing comments or function names, be explicit about what you want Copilot to do. For example, a comment like // Calculate the total price including tax
can help Copilot generate a relevant code snippet.
Also, you must not do all in one prompt. Generate and then tweak it out until it fits your needs and standards.
Trick Copilot with sample code
One trick to get Copilot on the right page, is to copy and paste sample code that is close to what you are looking for into your open editor. Providing a small example can help Copilot generate suggestions that match the language and tasks you want to achieve.
Once Copilot begins providing you with the code you want and will actually use, you can delete the sample code from the file. This can be especially helpful to jump-start Copilot to a newer library version when it defaults to providing older code suggestions.
If you didn’t used Github Copilot, you can try it for Free for a Month and see if it brings value to you.
GitHub Copilot · Your AI pair programmer · GitHub
After Free Trial, pricing is 10usd per month or 100usd per year.
Edit: As of today (18.12.2024.) its free for limited use, read more on:
Announcing a free GitHub Copilot for VS Code
In this first installment of our series on leveraging Copilot in Visual Studio Code, we explored effective code completion tips tailored for AL developers working with Business Central. Copilot’s intelligent suggestions can significantly boost productivity and streamline your development process.
Stay tuned as we continue to dive deeper into Copilot’s features and share more tips for mastering VS Code for AL. This is just the beginning of two series: Copilot and VS Code tips —there’s much more to come for AL developers looking to enhance their coding toolkit!
Subscribe to our email newsletter to get the latest posts delivered right to your email.
Comments