And write codes 3X Faster
On November 30th, 2022, the technology world experienced a major shift when ChatGPT chatbot was launched. It has taken the technology world by storm and could make traditional search engines like Google irrelevant with its ability to understand and respond to human language in a conversational manner and with unprecedented accuracy.
AI is changing the world the way Computers changed it in the 1980s and Internet changed it in the 1990s. And this blog post showcases on such example where AI can assist developers in writing code based on their intent, using the AI tool GitHub Copilot.
What is GitHub Copilot?
GitHub Copilot (GC) is an AI based code completion tool. A copilot is a person who assists the pilot in the cockpit. Similarly, GitHub Copilot assists the developer in writing code. It was developed by GitHub and OpenAI (the same company who is behind ChatGPT)
GC was first announced by GitHub on 29 June 2021 and was made available as a subscription-based service for individual developers on 22nd June 2022. It was made available for Enterprise customers on December 8th, 2022.
And what is amazing about this AI based tool is that you just state your intent as comments in the code and GC will suggest the code to complete the task. When you accept the code, it will be added to the code editor, and you can modify it as per your needs.
I took GC for a spin and experimented with two use cases to come up with Salesforce Apex Code. (It is available as free trial for 60 days, after which there is a subscription price of US$ 10 / month or US$ 100 / year.)
- The first one was a simple scenario where I wrote the code update the address of all Contacts for an Account based on the address of the Account.
- And the second was slightly more advanced using List, Set & Map features of Apex to populate the WhoId on Task records based on the Email Address.
In both the cases, I was able to write the code 3X faster with the help of GC and that too by a conservative estimate. (Please refer to the video below)
What I liked about GitHub Copilot?
- The code suggestions were generated based on the comments written in plain English.
- Complete blocks of code were suggested, and I was able to use them as is in most cases, reducing the time type the code.
- The correct API names of the objects and fields were suggested, specifically for standard objects and fields in Salesforce, reducing the time to type the API names.
- For custom objects and fields, GC came up with the API names based on the comment and even added ‘__c’ to the API name. But since the API name in my Salesforce Org was different, I corrected the suggested name. And once I did that, GC AI used the correct API name in subsequent blocks of code. Very cool!
Concerns about GitHub Copilot
- While GC AI was able to write the code with amazing accuracy, it wasn’t 100% correct all the time. In some tests, it added an “update” statement inside the loop, which is not a best practice. So, you do need to understand the code that it generates and may need to fine tune it.
- Any code that is written using GitHub Copilot may be subject to copyright if it incorporates existing code that is protected by copyright.
- GC collects user engagement data that may include personal data. Please make sure that you understand what data is collected and how it is used and shared before deciding to use it for work.
My suggestion, while you should be okay to play with it for your learning and experiment, please take the approval of your organization / client before using it for work.
If you are new to Apex, GitHub Copilot can be a great way to learn the language. Just by typing what you want to do as comments, you will be able to see the blocks of code that GC AI is suggesting. And if you are an experienced developer, GC is a great way to write code faster.
Here is a step by step guide on how to use GitHub Copilot in writing Salesforce Apex Code.
And here is a short video (8 minutes) showing you a demo of writing Salesforce Apex Code with and without using GC AI, and the time it takes.