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

GitHub
1 Dec 202329:53

TLDRChristopher Harrison, a senior developer advocate at GitHub, provides an in-depth guide on utilizing GitHub Copilot for efficient coding. He emphasizes that Copilot is a tool that assists by generating code suggestions based on the context of the code and comments typed by the user. Harrison outlines the importance of understanding how to phrase prompts effectively to guide Copilot in providing accurate suggestions. He also highlights the significance of providing clear, specific instructions and examples to ensure Copilot's suggestions align with the developer's intent. The video covers best practices such as describing the goal at the top of the file, being flexible with the probabilistic nature of the tool, and using proper naming conventions to aid Copilot in understanding the code structure. Harrison concludes by encouraging developers to go with the flow and adapt to the dynamic suggestions of GitHub Copilot, which can significantly enhance productivity and maintain a steady workflow.

Takeaways

  • 🛠️ **GitHub Copilot as a Tool**: Recognize GitHub Copilot as a tool that assists in coding, not a magic solution, and spend time understanding how to maximize its utility.
  • 💡 **Behind the Scenes**: Understand how GitHub Copilot works by sending context to the server, which then uses OpenAI to generate suggestions based on your code and comments.
  • 📄 **Context Matters**: Be aware that the entire file you're working on, along with open tabs in your IDE, are considered as context by GitHub Copilot to generate relevant suggestions.
  • 📑 **Focus on Relevant Tabs**: Ensure that the tabs open in your IDE are the ones most relevant to your current task, as they significantly influence the context provided to Copilot.
  • 📝 **Prompt Crafting**: Learn to write clear, concise, and specific prompts to guide GitHub Copilot in generating the code suggestions you need.
  • 🔍 **Be Specific with Intent**: Clearly state your intent and goals within your comments to help GitHub Copilot understand and predict what you are trying to achieve.
  • 🧩 **Use Examples**: Provide examples when possible to give GitHub Copilot a clearer understanding of the expected output or behavior.
  • 📌 **Naming Conventions**: Use clear and descriptive variable names to help GitHub Copilot make better suggestions, as it pays attention to the context provided by your code.
  • 🔄 **Be Flexible**: Understand that GitHub Copilot is probabilistic, and be prepared to rephrase or adjust your prompts if the suggestions do not meet your expectations.
  • ✅ **Good Code Practices**: Follow best practices in coding, as GitHub Copilot learns from the patterns and styles in your code to provide better suggestions.
  • 🚀 **Go with the Flow**: Embrace the dynamic nature of working with GitHub Copilot, adapting and adjusting as you receive feedback and suggestions.

Q & A

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

    -GitHub Copilot is a suite of tools primarily focused on code completion. It provides an embedded experience inside your IDE or editor, generating suggestions for code and comments based on the context it receives.

  • How does GitHub Copilot use the context provided by the user to generate suggestions?

    -As the user types code or comments, the context is sent to GitHub Copilot. It uses the OpenAI Codex to understand the user's intent and generates suggestions accordingly, which are then sent back to the user's IDE.

  • What is considered as part of the context when using GitHub Copilot?

    -The context includes the entire file the user is working on, not just from the cursor upwards, and the tabs that are open within the IDE, focusing on the most relevant ones based on the current task.

  • Why is it important to be specific when crafting prompts for GitHub Copilot?

    -Being specific helps GitHub Copilot understand the user's exact requirements, leading to more accurate and relevant suggestions. It avoids ambiguity and ensures that the AI tool provides the desired outcome.

  • What is the significance of using clear and descriptive variable names when working with GitHub Copilot?

    -Clear and descriptive variable names help GitHub Copilot to better understand the context and intent of the code being written. It aids in generating more accurate suggestions and follows the best practice of writing maintainable code.

  • How can providing examples assist GitHub Copilot in generating code?

    -Providing examples gives GitHub Copilot a clearer understanding of the expected outcome. It helps the tool to recognize patterns and structures, leading to more precise and tailored suggestions.

  • What is the role of 'prompt crafting' in utilizing GitHub Copilot effectively?

    -Prompt crafting involves phrasing comments and code in a way that maximizes the effectiveness of GitHub Copilot. It includes providing context, clarity, intent, and specificity to guide the AI towards generating the desired suggestions.

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

    -Leaving the comments helps document how the code was generated, which can be useful for future reference or when fine-tuning the model. It serves as an artifact of the development process and aids in maintaining the code.

  • How does GitHub Copilot handle the concept of 'protect' in the context of Django models?

    -When the user specifies 'protect' in the context of Django models, GitHub Copilot interprets this as a requirement to prevent the deletion of a primary object if there are dependent objects (children), thus adhering to the specified constraint.

  • What is the importance of being flexible when using GitHub Copilot?

    -Being flexible allows the user to adapt to the probabilistic nature of GitHub Copilot's suggestions. It encourages rephrasing or reframing the prompt if the initial suggestion does not meet the expectations, leading to a more effective use of the tool.

  • How can one ensure that GitHub Copilot follows the team's or project's specific coding conventions?

    -By demonstrating the desired coding style through examples in the code, GitHub Copilot can learn and follow the specific conventions. It adapts to the user's coding patterns and best practices, ensuring consistency with the team or project standards.

Outlines

00:00

🌟 Introduction to GitHub Copilot

Christopher Harrison, a senior developer advocate at GitHub, introduces himself and expresses excitement about discussing GitHub Copilot. He emphasizes the tool's nature, reminding the audience that despite its advanced capabilities, it is a tool that can be optimized for better use. Harrison outlines his plan to delve into how GitHub Copilot works and provide tips and tricks for effective utilization. He introduces GitHub Copilot as a suite of tools, primarily focusing on code completion, which generates suggestions based on the context provided by the user's code and comments.

05:04

📘 Understanding GitHub Copilot's Context

The paragraph discusses how GitHub Copilot uses the context of the code and comments being typed to generate suggestions. It clarifies that the entire file the user is working on, as well as open tabs in the IDE, are considered as context. Harrison dispels the misconception that the entire project context is used, highlighting the importance of focusing on the most relevant files. He also touches on the natural way developers work with files and how GitHub Copilot intelligently prioritizes the context based on the open tabs and the user's workflow.

10:04

💡 Prompt Crafting for GitHub Copilot

Harrison introduces the concept of 'prompt crafting', which involves phrasing comments and code in a way that maximizes the utility of GitHub Copilot. He breaks down prompt crafting into components such as context, intent, clarity, and specificity. The paragraph emphasizes the importance of clear and specific prompts, using the analogy of a simple ice cream request to illustrate the need for detailed instructions. Harrison also cautions against the assumption that GitHub Copilot can understand vague or incomplete prompts and encourages developers to provide clear and comprehensive prompts for better results.

15:06

🔍 Best Practices with GitHub Copilot

The speaker outlines best practices for using GitHub Copilot effectively. He advises describing the goal at the beginning of the file, being flexible due to the probabilistic nature of the tool, and providing examples to guide the tool's suggestions. Harrison demonstrates these practices using a Python Django application, showing how to build models and utilize GitHub Copilot's suggestions. He also shows how to be specific in requests and how the tool can learn from the user's actions, adapting its suggestions accordingly.

20:08

🔄 Adjusting and Learning with GitHub Copilot

Harrison discusses the importance of proper naming conventions and good code practices when using GitHub Copilot. He explains how the tool can learn from the user's code and adapt its suggestions, using the example of preventing a speaker from being deleted if they have associated talks. He also addresses a common question about maintaining naming conventions, suggesting that showing GitHub Copilot examples of desired code will help it follow along. The paragraph concludes with a demonstration of creating views and optimizing database queries using GitHub Copilot's capabilities.

25:12

📝 Final Tips and Conclusion

In the final paragraph, Harrison reiterates the importance of leaving comments in the code as artifacts of how the code was generated, which can be helpful for future adjustments. He reinforces the idea that good code practices will lead to better suggestions from GitHub Copilot. The speaker emphasizes the need to be flexible and go with the flow when using the tool, adapting to its probabilistic nature. He concludes by thanking the audience for their time and summarizing the key takeaways from the presentation.

Mindmap

Keywords

💡GitHub Copilot

GitHub Copilot is an AI-powered tool that assists developers by providing code suggestions and completions. It is designed to enhance the coding process by generating code snippets based on the context provided by the developer, such as comments or partial code. In the video, GitHub Copilot is used to create models and views for a Django web application, showcasing how it can streamline the development workflow.

💡Code Completion

Code completion refers to the feature of automatically completing code for a developer as they type. This feature is central to GitHub Copilot's functionality, which analyzes the context of the code being written and suggests the next logical lines of code. It is portrayed in the video as a way to increase efficiency and reduce the need to look up syntax or navigate away from the current task.

💡Context

In the context of GitHub Copilot, context is the information that the tool uses to generate suggestions. This includes the entire file being edited, any open tabs in the integrated development environment (IDE), and the current line of code or comment. The video emphasizes the importance of context in guiding the AI to provide relevant and accurate 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 context at different times. The video script mentions this to manage user expectations and to encourage flexibility when using the tool.

💡Prompt Crafting

Prompt crafting is the technique of carefully phrasing comments and code in a way that maximizes the effectiveness of GitHub Copilot. It involves providing clear, specific, and unambiguous prompts to guide the AI towards generating the desired code. The video discusses the importance of prompt crafting in helping developers get the most out of the tool.

💡Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. In the video, the speaker uses Django to create a web application, demonstrating how GitHub Copilot can be utilized within the framework to generate models, views, and other necessary components for a web application.

💡Models

In Django, models are the components that represent the data and the rules that govern access to that data. The video script includes a demonstration of using GitHub Copilot to create models for a conference website, which includes a speaker, track, and talk, highlighting how the tool can assist in defining data structures and relationships.

💡Views

Views in Django are functions or classes that coordinate the logic of how data should be presented to a user. The video shows how GitHub Copilot can be used to generate view code, which is essential for rendering data in a web application's frontend.

💡Regular Expressions

Regular expressions are patterns used to match character combinations in strings. In the context of the video, regular expressions are used to define the format of an 'abbreviation' field in a model. GitHub Copilot generates a regular expression to enforce that the abbreviation consists of exactly three uppercase letters.

💡F Strings

F Strings are a feature in Python that provides a way to embed expressions inside string literals, using a minimal syntax. The video demonstrates the use of F Strings as a method to format strings, specifically when generating a 'code' field for a model based on a combination of track, level, and ID.

💡Lazy Loading

Lazy loading is a design pattern commonly used in programming where a system initially provides a simplified version of an object and only fetches the full object when necessary. In the video, the concept is used to describe the default behavior of Django when displaying related data, which can lead to inefficient database queries. The speaker then shows how to optimize this with code suggestions from GitHub Copilot.

Highlights

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

Context for code suggestions includes the entire file you're working on and open tabs in your IDE.

GitHub Copilot uses the OpenAI Codex model to generate suggestions based on your code and comments.

Prompt crafting is essential for effectively using GitHub Copilot; it involves clear, specific, and contextual phrasing.

Describing the goal at the top of the file can help guide GitHub Copilot's suggestions.

Being flexible with GitHub Copilot's suggestions allows for better adaptation and utilization of the tool.

Providing examples can significantly improve the accuracy of suggestions from GitHub Copilot.

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

GitHub Copilot learns from your coding patterns and the code you write, adapting to your style and conventions.

Having relevant files open can improve the context GitHub Copilot uses to make suggestions.

The importance of leaving comments in the code for artifact purposes and to guide future adjustments.

GitHub Copilot can automatically generate regular expressions based on the description provided in comments.

The tool can help avoid common lookups for syntax by providing suggestions that keep the developer in the zone.

Using clear and specific prompts can prevent misunderstandings and improve the suggestions provided by GitHub Copilot.

GitHub Copilot can help in creating dynamic fields by understanding the examples and patterns described in comments.

The tool can automatically import modules related to the models you're working on, based on open files.

Good code practices like proper naming and clear intent can enhance the performance and suggestions of GitHub Copilot.

The concept of 'going with the flow' with GitHub Copilot means adapting to its probabilistic nature and working along with it.