Skip to main content

Insight | Mar 12, 2024

maze

Do's and Don'ts of GitHub Copilot (For Drupal Developers)

By Brent Schultz

To get the most from GitHub Copilot as your Drupal coding sidekick, know its strengths and limits. Its AI can spit out boilerplate code, but it's no replacement for developer know-how. 

Here's a quick guide to help Copilot help you.

Do 

Give it explicit direction on a line-by-line basis for mundane asks.

This is where nearly all of your efficiency gain will come from. An argument could be made for simple code completion in your IDE being nearly as effective, and I can see both sides. Your mileage may vary.

github code
github code

Proofread all the things.

Never assume it’s going to be completely correct. Even the simplest things have some nuance that can be less than ideal or incorrect. Very much related to this…

Be cognizant of “Contextual Tab” logic. 

Copilot uses up to the five “nearest” tabs to the file you’re working in (VS Code) to provide context to your AI generation. This can be extremely helpful or harmful. The below code was written entirely from the prompt // implement a preprocess hook for the page.

It nailed the preprocess function name. Awesome! 

The rest of the logic it dumped is 1:1 copied from a neighboring module file I had open. Nowhere in the prompt did I ask it to do any of the things it tried to do? This is contextual tabs gone wrong.

github code

Use it for Javascript, SQL, or other cross-platform and CMS agnostic technology. 

Copilot’s effectiveness, like all AI and LLM systems, relies heavily on the amount of data it can consume to curate valuable responses. Based on the sheer quantity of libraries and repos, Javascript has a leg up on something like Drupal. SQL is so standardized and slow-moving that things like Copilot can be nearly all-knowing based on the massive amount of up-to-date information it has available.

Understand that Copilot will give you output even if it understands 60% of your prompt. 

Pay close attention to your prompts vs outputs. This does not mean that Copilot is always wrong, but it does mean that your prompt could be misleading or be only somewhat understood. A great example I uncovered trying to architect this scenario:

  • Example 2 and 3: Unpublished != Draft status. Unpublished could be one of many states that are not “Publish,” aka. 1
  • Example 1: “All” newsroom articles could sensibly mean all published articles, but that’s not technically “All,” is it?
github code

Provide context in one of the many ways available:

  1. Open files
    Copilot looks at the current and open files in your editor to analyze the context and create appropriate suggestions. Having related files open in VS Code while using Copilot helps set this context and lets the Copilot see a bigger picture of your project.
     
  2. Top level comment
    Just as you would give a brief, high-level introduction to a coworker, a top level comment in the file you're working in can help Copilot understand the overall context of the pieces you will be creating.
     
  3. Appropriate includes and references
    It's best to manually set the includes or module references needed  for your work. Copilot can make suggestions, but you likely know best what dependencies you must include. This can also help let Copilot know what frameworks, libraries, and their versions you'd like it to use when crafting suggestions.
     
  4. Meaningful function names
    Just as a method called fetchData() won't mean much to a coworker (or you after several months), fetchData() won't help Copilot either. Using meaningful function names will help Copilot provide a body that does what you want.
     
  5. Specific and well-scoped function comments
    A function name can only be so descriptive without being overly long, so function comments can help fill in details that Copilot might need to know.

Don’t

Expect magic to be made. 

This is where the bulk of AI “Mistakes” happen. I ask it to write a preprocess function that checks the current path. It wrote a function that does exactly that, with some added sauce. The library it attached doesn’t exist, and if I had to guess, it got this idea from a neighboring tab. It does nothing to verify the path exists. Frankly, if it ever gets that smart, robots might replace us. Until then, though, proofread all the things!

github code

Expect it to do much for you in a Twig file.

I tried nearly every simple prompt I could, and it seemed to be completely lifeless. It did a great job of using a huge amount of CPU, though! I have a fun video to share of me making heavy use of the Activity Monitor to kill processes if you’d like to see how it goes.

Expect flawless accuracy with anything, but especially with CMS-specific logic or understanding.

Copilot does a great job of using the right methods to do specific actions, but the nuance and intent are often lost. This still points back to proofreading and the necessity for your knowledge to be adequate. It will not teach you new things. If you see a new approach, you should read it five times over to ensure it’s doing what you want.

Drop us a line

Have a project in mind?

Contacting Third and Grove may cause awesomeness. Side effects include a website too good to ignore. Proceed at your own risk.

Reduced motion disabled