Coding with an AI pair programmer: Getting started with GitHub Copilot

GitHub
1 Dec 202329:53

TLDRChristopher Harrison, a senior developer advocate at GitHub, introduces GitHub Copilot, a suite of tools for code completion. He explains that Copilot uses the context of the code and comments being typed to generate suggestions via the OpenAI model. Harrison emphasizes the importance of understanding Copilot as a tool and offers tips to maximize its utility, such as providing clear and specific prompts, describing the goal at the top of the file, being flexible with the probabilistic nature of the tool, and leaving comments for documentation. He also highlights the significance of good variable naming and code practices, as they influence the quality of suggestions. The talk concludes with a live coding demonstration using Python Django to illustrate the application of these tips.

Takeaways

  • πŸ”§ **GitHub Copilot as a Tool**: Recognize GitHub Copilot as a tool, not magic, and spend time learning how to maximize its utility.
  • πŸ€– **AI Behind the Scenes**: Understand how GitHub Copilot uses the context of your code and comments to generate suggestions through the OpenAI model.
  • πŸ“„ **Context Matters**: Be aware that the entire file you're working on, along with open tabs in your IDE, are considered context and sent to GitHub Copilot.
  • πŸ“‘ **Tab Focus**: Utilize the relevance of open tabs to guide GitHub Copilot's focus on the most pertinent files for your current task.
  • πŸ“ **Prompt Crafting**: Learn to write clear, concise, and specific prompts to guide GitHub Copilot in generating the desired code suggestions.
  • 🎯 **Be Specific**: Provide detailed information in your prompts to increase the accuracy of the suggestions, avoiding ambiguity.
  • πŸ”„ **Be Flexible**: Accept that GitHub Copilot's suggestions are probabilistic and be ready to rephrase or adjust your prompts for better results.
  • πŸ“ˆ **Provide Examples**: Including examples in your prompts can help GitHub Copilot understand and replicate the desired pattern or structure.
  • 🏷️ **Naming Conventions**: Use clear and descriptive variable names to help GitHub Copilot make better suggestions based on your coding context.
  • πŸ“ˆ **Learn from Patterns**: GitHub Copilot can learn from your coding patterns and adjust its suggestions accordingly, so be consistent in your approach.
  • 🌊 **Go with the Flow**: Embrace the dynamic nature of working with an AI tool and adapt your workflow to align with GitHub Copilot's capabilities.

Q & A

  • What is GitHub Copilot and what does it primarily focus on?

    -GitHub Copilot is a suite of tools that primarily focuses on code completion. It provides an embedded experience inside your IDE, generating suggestions for code and comments based on the context of what you are typing.

  • How does GitHub Copilot use context to generate suggestions?

    -As you type code or comments, the context, which includes the entire file you are working on and the open tabs in your IDE, is sent to GitHub Copilot. It then uses the OpenAI Codex to understand what you are trying to do and generates suggestions accordingly.

  • What is the significance of the tabs open in your IDE when using GitHub Copilot?

    -The tabs open in your IDE are significant because GitHub Copilot uses them to focus on the most relevant files related to your current task. It starts with the tabs closest to the one you are working on and works its way out.

  • What is 'prompt crafting' and why is it important when using GitHub Copilot?

    -Prompt crafting is the technique of phrasing and typing out your comments and code in a way that maximizes the effectiveness of GitHub Copilot. It involves providing clear, specific, and contextually relevant information to guide the AI towards generating the most appropriate suggestions.

  • How can you ensure that GitHub Copilot follows your team's coding conventions?

    -You can ensure GitHub Copilot follows your team's coding conventions by demonstrating the conventions through examples in your code. As GitHub Copilot learns from the code it assists with, it will adapt to and follow the patterns and styles you use.

  • What is the role of variable naming in getting good suggestions from GitHub Copilot?

    -Good variable naming is crucial for getting accurate suggestions from GitHub Copilot. Clear and descriptive variable names help the tool understand the context and intent of the code, leading to more relevant and useful suggestions.

  • Why is it recommended to leave the comments used for generating code with GitHub Copilot in the codebase?

    -It is recommended to leave the comments in the codebase because they serve as artifacts that explain how the code was generated. These comments can be helpful for future reference or when fine-tuning the model, as they provide a clear understanding of the developer's intent.

  • How does being flexible with GitHub Copilot's suggestions help in the coding process?

    -Being flexible allows you to adapt to the probabilistic nature of GitHub Copilot's suggestions. If the first suggestion isn't what you're looking for, you can rephrase your prompt or provide additional context, which can lead to better and more accurate suggestions.

  • What is the importance of specificity when crafting prompts for GitHub Copilot?

    -Specificity is important because it helps GitHub Copilot understand exactly what you want. By providing detailed and precise information in your prompts, you guide the AI to generate more accurate and relevant code suggestions.

  • How can providing examples in your prompts enhance GitHub Copilot's performance?

    -Providing examples can significantly enhance GitHub Copilot's performance by offering clear illustrations of the desired outcome. This helps the AI to better understand the task and generate suggestions that closely match your requirements.

  • What is the benefit of using good coding practices when working with GitHub Copilot?

    -Using good coding practices, such as clear variable names and following best practices, not only makes the code more readable for other developers but also helps GitHub Copilot generate better suggestions. The tool can more accurately interpret the code's intent when coding standards are adhered to.

Outlines

00:00

🌟 Introduction to GitHub Copilot

Christopher Harrison introduces himself as a senior developer advocate at GitHub and expresses excitement about discussing GitHub Copilot. He emphasizes that despite seeming magical, Copilot is a tool that can be optimized for better use. The paragraph outlines the basic function of GitHub Copilot, which is to provide code completion suggestions based on the context of the code and comments being typed. The process involves sending the context to GitHub Copilot, which then uses the OpenAI Codex model to generate and suggest code. The importance of understanding the tool and its context usage is highlighted, including the fact that the entire file, not just the cursor's current position, influences the suggestions.

05:04

πŸ“– Understanding GitHub Copilot's Context

The paragraph delves into the specifics of what 'context' means for GitHub Copilot. It clarifies that the tool considers the entire file being worked on, as well as open tabs in the editor, to determine the most relevant information for generating suggestions. Harrison dispels the misconception that Copilot considers the entire project. He also advises against micromanaging the tabs and instead suggests letting the tool naturally focus on the most related files as work progresses. The paragraph concludes with the idea that if a developer is shifting tasks or needs to introduce a new framework, they might need to manually adjust the open files to guide Copilot's context.

10:04

πŸ’‘ Prompt Crafting for GitHub Copilot

Harrison introduces the concept of 'prompt crafting' as a method to effectively communicate with GitHub Copilot. He breaks down the process into components: providing context, specifying intent, ensuring clarity, and being specific about what is needed. The paragraph uses the analogy of buying ice cream to illustrate the importance of specificity in prompts. It also emphasizes the probabilistic nature of the tool, suggesting that developers should be prepared to rephrase or provide examples if the initial suggestions are not on point. Harrison demonstrates how to use these principles in a Django application by describing the goal, being flexible, and providing examples to guide Copilot's suggestions.

15:06

πŸ” Navigating GitHub Copilot's Suggestions

The paragraph discusses how to effectively use GitHub Copilot's suggestions. It advises on the importance of proper naming conventions, which not only aids other developers but also helps Copilot understand the code's intent. Harrison demonstrates how to correct a relationship setting in Django models to prevent unintended data deletion. He also shows how to use the tool's ability to learn from the developer's actions, such as changing the cascade deletion behavior to 'protect'. The paragraph concludes with a reminder to use good coding practices to enhance the effectiveness of Copilot's suggestions.

20:08

πŸ“š Training GitHub Copilot with Code Examples

Harrison addresses a common question about ensuring that GitHub Copilot follows specific coding conventions. He suggests that the best way to achieve this is by providing the tool with examples of the desired code structure. The paragraph also emphasizes the importance of opening relevant files to guide the suggestions. Harrison demonstrates creating views in Django and using comments to guide Copilot in generating more efficient database queries through 'select_related'. The paragraph concludes with the idea that being flexible and providing clear examples can help developers get the most out of GitHub Copilot.

25:12

πŸ”„ Flexibility and Best Practices with GitHub Copilot

The final paragraph reiterates the importance of flexibility when using GitHub Copilot, as the tool's probabilistic nature may yield different suggestions in the same scenario. Harrison advises leaving the comments in the code as they serve as artifacts of how the code was generated. He also stresses the importance of good code practices, such as proper naming and following best practices, which will naturally lead to better suggestions from Copilot. The paragraph concludes with the overarching theme of going with the flow and adapting to the tool's suggestions while also guiding it with clear and specific prompts.

Mindmap

Keywords

GitHub Copilot

GitHub Copilot is an AI-powered tool developed by GitHub that assists developers by providing code completion suggestions as they write code. It uses machine learning models to understand the context of the code and generate relevant suggestions. In the video, it is the central tool being discussed, with the presenter, Christopher Harrison, explaining how to maximize its utility in coding.

Code Completion

Code completion refers to the feature of automatically completing code for a programmer as they begin typing, often used in integrated development environments (IDEs). In the context of the video, GitHub Copilot provides code completion by generating suggestions for the next lines of code based on the current context and the developer's input.

Context

In the script, context is the information that GitHub Copilot uses to generate code suggestions. This includes the entire file a developer is working on, as well as open tabs in the IDE that are related to the task at hand. The context helps GitHub Copilot understand what the developer is trying to achieve and provide relevant code suggestions.

Probabilistic

Probabilistic refers to the nature of GitHub Copilot's suggestions, which are based on probabilities rather than certainties. This means that the tool may offer different suggestions for the same prompt on different occasions. The video emphasizes the need for developers to be flexible and adapt to the suggestions provided by GitHub Copilot.

Prompt Crafting

Prompt crafting is a technique discussed in the video where developers deliberately structure their comments and code prompts to guide GitHub Copilot more effectively. By being clear, specific, and providing examples, developers can improve the quality of the suggestions they receive from the tool.

Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. In the video, the presenter uses Django to create a web application, demonstrating how GitHub Copilot can assist in generating models, views, and other components of a Django application.

Foreign Key Relationship

A foreign key relationship in the context of databases is a link between two tables, where one table points to a primary key in another table. In the video, the presenter discusses setting constraints on foreign keys to prevent deletion of a primary entity if there are related entities, which is a common requirement in database integrity.

Regular Expression

A regular expression is a sequence of characters that defines a search pattern, which can be used for string searching and manipulation. In the video, the presenter instructs GitHub Copilot to generate a regular expression for validating an abbreviation field, demonstrating the tool's ability to handle complex patterns.

Lazy Loading

Lazy loading is a design pattern commonly used in programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in application performance. In the video, the concept is mentioned in relation to Django's default behavior when loading related data from a database.

Computed Field

A computed field is a field in a database or a model whose value is not stored but is calculated from other fields when needed. In the video, the presenter creates a computed field for a 'code' attribute in a model, which is derived from the track, level, and ID of a talk.

F String

An F string is a feature in Python 3.6 and later, which allows for embedded expressions inside string literals, using f'' prefix. In the video, the presenter uses F strings to format a computed field in a Django model, demonstrating a concise way to include variables in strings.

Highlights

GitHub Copilot is a suite of tools primarily focused on code completion.

Context is sent to GitHub Copilot as you type, which then generates suggestions based on the information.

GitHub Copilot uses the OpenAI Codex model to understand your code and comments, and generate relevant suggestions.

The entirety of the file you are working on is considered as context, not just the text around the cursor.

Open tabs in your IDE are also taken into account to determine the most relevant context for suggestions.

Prompt crafting is crucial for effectively using GitHub Copilot; it involves phrasing your comments and code in a way that provides clear context and intent.

Being specific in your prompts helps GitHub Copilot generate more accurate suggestions.

Describing the goal at the beginning of your file can help guide the suggestions provided by GitHub Copilot.

GitHub Copilot is probabilistic, so it may provide different suggestions in the same context at different times.

Providing examples can help GitHub Copilot understand the expected output more clearly.

Good variable names and following best practices in coding can lead to better suggestions from GitHub Copilot.

GitHub Copilot can learn from the code patterns you write and follow your lead in terms of style and conventions.

It's important to be flexible and willing to rephrase or modify your prompts if the suggestions are not as expected.

Having relevant files open can help GitHub Copilot provide more accurate and contextually relevant suggestions.

GitHub Copilot can automatically import modules based on the context of your open files and the code you are writing.

The tool can help generate complex expressions, like regular expressions for validating fields, without having to look them up.

By demonstrating the desired code structure and patterns, GitHub Copilot can adapt to and follow your project's specific conventions.

Leaving comments in your code as artifacts of how the code was generated can be helpful for future reference and fine-tuning.

The key to effectively using GitHub Copilot is to go with the flow, be flexible, and continuously refine your prompts and code.