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

GitHub
1 Dec 202329:53

Summary

TLDRIn this insightful presentation, Christopher Harrison, a senior developer advocate at GitHub, delves into the capabilities of GitHub Copilot, a suite of AI-powered tools designed to enhance coding efficiency. Harrison emphasizes the importance of treating Copilot as a tool, not magic, and offers practical tips for optimizing its use. He covers the tool's behind-the-scenes operation, discusses the significance of context in code suggestions, and introduces the concept of 'prompt crafting' to guide developers in phrasing their code and comments effectively. Through a demo using Python and Django, Harrison illustrates how to leverage GitHub Copilot for code completion, highlighting the need for clear intent, specificity, and flexibility in interactions with the tool.

Takeaways

  • ๐ŸŒŸ GitHub Copilot is a suite of tools primarily focused on code completion, providing suggestions based on the context of the code and comments you write.
  • ๐Ÿ›  It's important to remember that GitHub Copilot is a tool, not magic, and optimizing its use can enhance your development workflow.
  • ๐Ÿค– Behind the scenes, GitHub Copilot sends the context of your code and comments to an AI model, which then generates suggestions tailored to your coding task.
  • ๐Ÿ“„ The 'context' includes the entire file you're working on, as well as other open tabs in your IDE, focusing on those most relevant to your current task.
  • ๐Ÿ” There can be confusion about what data is sent to GitHub Copilot, but it primarily uses the open file and related open tabs to inform its suggestions.
  • ๐Ÿ’ก Prompt crafting is a technique to effectively communicate with GitHub Copilot by being clear, specific, and intentional in your comments and code.
  • ๐Ÿ“ Describing the goal at the top of the file helps set the context for GitHub Copilot and can improve the relevance of its suggestions.
  • ๐Ÿ”„ Being flexible with GitHub Copilot's suggestions is crucial, as the AI's responses are probabilistic and may vary.
  • ๐Ÿ“š Providing examples in your comments can guide GitHub Copilot to understand and replicate the patterns you're looking for in your code.
  • ๐Ÿ”‘ Good coding practices, such as proper naming conventions, can help GitHub Copilot generate more accurate and useful suggestions.
  • ๐Ÿ›ฃ๏ธ Going with the flow and adapting to GitHub Copilot's suggestions can lead to a more efficient and productive development process.

Q & A

  • Who is Christopher Harrison and what is his role?

    -Christopher Harrison is a senior developer advocate at GitHub, and he is excited to talk about GitHub Copilot and how to maximize its utility.

  • What is GitHub Copilot?

    -GitHub Copilot is a suite of tools primarily focused on code completion. It provides suggestions for code and comments based on the context of what the user is typing in their integrated development environment (IDE).

  • How does GitHub Copilot generate suggestions?

    -GitHub Copilot sends the context of what the user is typing to its servers, where it uses OpenAI's technology to understand the user's intent and generate relevant suggestions that are then sent back to the user's IDE.

  • What context does GitHub Copilot consider when generating suggestions?

    -GitHub Copilot considers the entire file the user is working on, including the content from the beginning to the end of the file, as well as the tabs that are open in the user's IDE, focusing on those most relevant to the current task.

  • Why is it important to understand that GitHub Copilot is a tool?

    -Recognizing GitHub Copilot as a tool helps developers to manage their expectations and spend time learning how to use it effectively, rather than expecting it to perform magical or perfect tasks.

  • What is the concept of 'prompt crafting' mentioned in the script?

    -Prompt crafting refers to the method of phrasing and typing comments and code in a way that maximizes the effectiveness of tools like GitHub Copilot, by providing clear context, intent, clarity, and specificity.

  • Why should developers be flexible when using GitHub Copilot?

    -Developers should be flexible because GitHub Copilot operates on a probabilistic model, meaning it may provide different suggestions in the same scenario at different times. Being flexible allows developers to rephrase or adjust their approach to get the desired suggestions.

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

    -Proper variable naming is significant because GitHub Copilot reads and interprets variable names to understand the context and provide relevant suggestions. Clear and descriptive variable names help the tool to generate more accurate and useful code completions.

  • How can providing examples help GitHub Copilot to understand a developer's intent?

    -Providing examples gives GitHub Copilot concrete instances of what the developer is trying to achieve, which helps the tool to understand the specific requirements and generate suggestions that align more closely with the developer's intent.

  • What is the recommendation for handling the comments used to guide GitHub Copilot after the code has been written?

    -The recommendation is to leave the comments behind as they serve as artifacts of how the code was generated. They can be helpful for future reference and for fine-tuning the model later on.

  • What are some best practices for using GitHub Copilot effectively?

    -Best practices include describing the goal at the top of the file, being flexible with the suggestions provided, providing examples to guide the tool, naming variables properly, following best practices in coding, and going with the flow by being adaptable and open to the tool's suggestions.

Outlines

The video is abnormal, and we are working hard to fix it.
Please replace the link and try again.

Mindmap

Keywords

๐Ÿ’กGitHub Copilot

GitHub Copilot is a suite of tools developed by GitHub that primarily focuses on code completion. It integrates into a user's code editor, providing suggestions for both code and comments based on the context of what the user is typing. In the video, it's highlighted as a powerful tool that can help developers write code more efficiently by suggesting what they might be looking for, thus keeping them in the 'zone' and reducing the need to look up syntax or other information.

๐Ÿ’กCode Completion

Code completion refers to the feature within GitHub Copilot that suggests possible code snippets as a developer types. It's designed to enhance productivity by reducing the time spent on writing repetitive code or looking up specific syntax. In the script, code completion is exemplified through the automatic suggestions that appear as the developer describes their intentions in comments or code.

๐Ÿ’กContext

In the context of GitHub Copilot, 'context' refers to the information that the tool uses to generate accurate suggestions. This includes the entire file a developer is working on, as well as the open tabs in their editor. The script emphasizes the importance of context in helping GitHub Copilot understand the developer's intent and provide relevant suggestions.

๐Ÿ’กProbabilistic

The term 'probabilistic' in the video describes the nature of GitHub Copilot's suggestions. It means that the tool provides suggestions based on probabilities and patterns, rather than deterministic outcomes. This is illustrated in the script when the speaker mentions that the same prompt might yield different suggestions on different days, reflecting the tool's learning and adaptation over time.

๐Ÿ’กPrompt Crafting

Prompt crafting is a technique discussed in the video that involves carefully phrasing comments and code to elicit the most useful suggestions from GitHub Copilot. It's about being clear, specific, and intentional in how one communicates their coding needs to the tool. An example from the script is when the developer specifies the creation of a 'speaker' model with 'name', 'email', and 'LinkedIn' to guide Copilot's suggestions.

๐Ÿ’กIDE (Integrated Development Environment)

An Integrated Development Environment (IDE) is a software application that provides comprehensive facilities for software development. In the video, the IDE is where GitHub Copilot operates, sending context to and receiving suggestions from GitHub Copilot. The script mentions that developers can accept, reject, or modify the suggestions provided by Copilot within their IDE.

๐Ÿ’กDjango

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. In the script, Django is used as an example to demonstrate how GitHub Copilot can assist in building models and views for a web application. The video showcases how Copilot can generate Django model code and understand the framework's conventions.

๐Ÿ’กRegular Expression

A regular expression is a sequence of characters that defines a search pattern, which can be used for string matching and manipulation. In the context of the video, GitHub Copilot generates a regular expression to enforce a specific format for a model field, such as ensuring an abbreviation consists of three uppercase letters.

๐Ÿ’กF-string

An F-string (formatted string literal) is a feature in Python that allows for easy and readable string formatting. The video script mentions F-strings when discussing how GitHub Copilot automatically uses this feature to format strings in a specific way, which can be adjusted by the developer for more specificity if needed.

๐Ÿ’กBest Practices

Best practices refer to the recommended methods or techniques in a particular field or activity. In the video, the speaker emphasizes following best practices in coding, such as using clear and descriptive variable names, as a way to improve the effectiveness of GitHub Copilot's suggestions. The script illustrates this with examples like using 'LinkedIn' instead of an abbreviation, which helps Copilot understand the developer's intent.

Highlights

The video is abnormal, and we are working hard to fix it.
Please replace the link and try again.

Transcripts

play00:00

Good morning.

play00:00

Good afternoon. Good evening.

play00:01

Depending on where in the world you happen to be, my name is Christopher Harrison.

play00:05

I'm a senior developer advocate at GitHub and I'm really excited to talk today

play00:10

about GitHub Copilot and about how to get the most out of this tool.

play00:15

That one thing that we will sometimes forget when we're looking at

play00:19

these tools is the fact that they are tools.

play00:23

They can often feel like magic.

play00:24

I type out a few words and while I've got this really, really cool thing,

play00:29

but at the end of the day, it's a tool just like our IDE,

play00:33

and we should spend a little bit of time seeing how we can get the most out of it.

play00:37

So I want to start off by talking a little bit

play00:40

about GitHub Copilot and how it works behind the scenes,

play00:43

and then we'll get into some tips and tricks

play00:46

that you can start to implement as you get in and explore the tool.

play00:52

So what is GitHub

play00:53

Copilot ? So GitHub.

play00:54

Copilot is a suite of tools.

play00:57

I'm going to be primarily focused in on code completion.

play01:01

So this is the embedded experience inside

play01:04

of your idea, inside of your editor, where as you add in comment,

play01:10

as you add in code, it's then going to generate suggestions, generate

play01:15

what it thinks that you are looking for based on that information.

play01:20

So how does it do its thing behind the scenes?

play01:23

Well, as you're typing out your code, as you're typing out your comments,

play01:28

context is sent up to GitHub.

play01:31

Copilot I'm going to talk about that context in just a moment,

play01:35

but from right now, let's focus in on that processing

play01:38

so that context is sent up to GitHub Copilot.

play01:41

GitHub Copilot then uses the OpenAI allow them

play01:45

behind the scenes, sees what it is that you're trying to do and then in turn

play01:50

generate suggestions and send those sends those down to your I.D.E.

play01:56

You can then accept those, reject those, accept and modify,

play02:00

use that as the foundation.

play02:02

And it really does help keep you in the zone and help

play02:06

you avoid having to go off and maybe look up obscure syntax and things like that.

play02:11

Now, let's talk a bit about that context, because a lot of times

play02:15

there's some confusion about this, exactly what is going to be sent up to GitHub

play02:21

Copilot? Well, the first and kind of the most obvious is the file that you are

play02:26

currently working on, and that's going to include the entirety of the file.

play02:31

So it's not just from where my cursor is and everything above it,

play02:35

but rather it's the start of the file, the bottom of the file,

play02:38

the middle of the file.

play02:40

So if I'm going in and I'm making updates, I'm adding new code,

play02:43

I'm making changes.

play02:45

It's going to look at the entirety of the file, not just from the cursor

play02:50

and above.

play02:52

The next big thing is the tabs

play02:54

that are open inside of your idea.

play02:58

This sometimes cause and causes confusion and I'll admit for me personally,

play03:03

it also caused a little bit of confusion because I honestly did think

play03:07

that it summed up the entirety of my project.

play03:10

And if we stop and think about that, that would be an awful lot of context

play03:14

for Copilot to try and figure out,

play03:17

try to parse out, try to figure out what's most important.

play03:21

Because like, if I think about, you know, reading

play03:25

a very long article, I want to focus in on what's most important.

play03:29

And if there's like, you know, 5000 words

play03:31

and this is really the only section that I'm interested in

play03:33

for this moment, the rest of that becomes a little bit superfluous.

play03:37

So we want to focus GitHub Copilot's attention on what's most important,

play03:42

and that's where those tabs come into play.

play03:46

It's going to focus in on what's most important based on those tabs.

play03:50

Additional.

play03:51

It's also going to start from the outside our inside and work its way out.

play03:57

So the tabs it will start with are the ones that are closest

play04:00

to the tab that I am currently working on. Now,

play04:06

one thing that sometimes people will do

play04:08

is they'll get a little bit too focused on trying to control the tabs,

play04:13

on trying to control the context that's going to be sent up.

play04:18

And honestly, if we think about the way that we typically write code,

play04:23

that if you're anything like me and I know I am, that

play04:27

I will open up a file, I'll make some modifications.

play04:30

I'm going to open up a related file,

play04:31

I'm going to make some modifications, I'm going to open up another related file.

play04:34

And this is normally the way that we wind up doing our work.

play04:38

So by having GitHub Copilot focus in on those tabs,

play04:42

it's typically naturally automatically going to get the files

play04:47

that are already most related to what it is that I'm doing now.

play04:52

If I'm shifting tasks, if I'm maybe going to be taking a look

play04:56

at something that's a new feature that I'll be implementing

play04:59

or if I know that I'm going to be working with a library or framework

play05:03

that Copilot has never seen before, then I might go ahead and

play05:08

very specifically open up a file, open up some representative example

play05:13

of what it is that I'm building, or maybe I'm switching tasks,

play05:16

or maybe I am not getting the suggestions that I'm looking for back

play05:21

from GitHub Copilot.

play05:23

Then at that point I might take another look at the files

play05:26

that I have open and maybe readjust at that point.

play05:29

But again, I really do want to highlight the fact that you will find

play05:33

most of the time just through your normal usage patterns

play05:37

of an idea of your editor, that you'll just inherently be opening up

play05:42

the files that are most closely related to what it is that you are working on.

play05:49

Now let's get in and start to

play05:51

kind of build upon that and see how we can get the most out of this tool.

play05:56

And I'm going to potentially introduce a term that's that's new

play05:59

you a thing called prompt crafting.

play06:02

And in a nutshell, this is really about how it is

play06:05

that we are going to phrase and type out our comment phrase

play06:10

and type out our code phrase and type out our prompts

play06:14

to get the most out of these tools that, as I highlighted at the start,

play06:19

I is a tool and I really do want to make sure

play06:23

that I've spent a little bit of time learning how to get the most out of it.

play06:26

Just like with my idea this is what prompt crafting is all about.

play06:32

Now, if we break this down into a couple of components

play06:36

about how it is that we can help GitHub Copilot help us.

play06:40

The first is going to be the context.

play06:43

This is that surrounding information.

play06:46

What is it that we're trying to build?

play06:48

How is it that we are trying to build it

play06:51

to start to then drill a little bit further down?

play06:54

The next is the intent.

play06:56

What is it that I'm doing?

play06:57

How is it that I'm trying to do?

play06:58

Why is it that I am trying to build that out?

play07:02

Next is the clarity.

play07:04

As a friend of mine likes to joke,

play07:07

don't use a big word when a diminutive one would use that.

play07:11

As I'm typing in my comment, as I'm trying to describe what it is

play07:17

that I'm doing, I want to make sure that I'm clear

play07:20

so I might start to avoid a lot of vernacular.

play07:24

I might try to avoid maybe abbreviations that make sense to me internally,

play07:30

but might not be as clear, might not

play07:33

define truly what it is that I am trying to build.

play07:37

Last but not least, is specificity.

play07:40

This is where I'm going to get in

play07:43

and identify exactly what it is that I want.

play07:46

Let's take, for example, the phrase Go get me some ice cream.

play07:50

Now I've provided enough information to complete that task.

play07:55

You know that you should go off and go grab some ice cream.

play07:58

That's wonderful.

play07:59

But what flavor of ice cream?

play08:02

What vessel should there be?

play08:03

Sprinkles. Don't know.

play08:06

So have I given enough information to complete the task?

play08:10

Yeah.

play08:11

Am I going to be satisfied with the result?

play08:13

Maybe not, because I'm going to be looking for salted caramel in a waffle cone.

play08:18

And the answer is always sprinkles.

play08:20

Yes, always sprinkles.

play08:22

So what I really wanted to do, if I was going to craft that out, say

play08:25

using Prime crafting, is make sure that I'm providing all of that

play08:29

specificity, all of that information upfront.

play08:32

And this is one of the big mistakes that I see developers make

play08:36

when they first get in and start playing around with copilot

play08:39

that they assume, I can just type out a couple of words, I can type out

play08:42

a couple of sentences, and voila, I'm going to get this very big thing.

play08:46

That's not really how it works that we want to make sure

play08:49

that we're providing good, clear prompts.

play08:53

I think the best way to do this is really start to put it into action.

play08:58

So before I get into this demo, I promise you there's going to be a demo

play09:02

here in just a moment.

play09:04

Let's take a look at a couple of real quick best practices.

play09:07

First up is describe the goal.

play09:10

I like to view this as adding a header, if you will, to the file.

play09:16

This doesn't have to be anything real in-depth per se, just three or four

play09:21

quick sentences describing what it is that I'm looking to build.

play09:26

If the framework isn't implied

play09:29

based on the file that I'm working on, maybe I'll add that in.

play09:32

You know, kind of what's the goal?

play09:34

How is it that I'm trying to do this?

play09:35

Just a couple, three lines up at the very top, add those in.

play09:40

Next is be flexible.

play09:42

We always need to remember that our tools are

play09:46

probabilistic, not deterministic.

play09:49

And so what this means is that I could be in the exact same scenario.

play09:54

So I can type out a comment, I could type out a phrase and get one suggestion.

play09:58

And then in that exact same scenario, the next day, I might get back

play10:03

something different.

play10:04

The tools are probabilistic.

play10:06

This is simply how these work.

play10:08

And that's all right.

play10:09

So I just need to be a little bit flexible.

play10:11

If I don't see what it is that I'm looking for the first time,

play10:14

maybe rephrase, reframe what it is that I am doing.

play10:19

And one big thing that you can do to really help out is to provide examples.

play10:25

So let's say that I'm going to be processing data.

play10:27

Let's say that I'm going to be doing some validation if I give a set of examples,

play10:33

then I'm going to be very clearly defining to GitHub Copilot

play10:37

what it is that I'm looking for.

play10:39

So let's take a look here.

play10:41

I've got an application that I'm going to be building out

play10:44

using Python, Django, Python, Django, one of my favorite frameworks.

play10:49

It's there to create frontend for backend databases.

play10:54

And the first thing that I need to do

play10:56

is to build out my models to describe my data here.

play11:00

So I'm going to start

play11:01

towards the top of my file here and describe what I'm doing.

play11:03

I'm building out a conference website, so build out a conference website

play11:09

and I'm going to have add a speaker,

play11:13

a track and a talk

play11:17

and make sure that parrot

play11:22

can't be deleted if there are children.

play11:26

So in other words, what I'm trying to specify here is

play11:30

if I have a primary foreign key relationship

play11:34

and I try to delete that primary, but there are foreign keys,

play11:37

don't allow those to be deleted.

play11:39

So just a couple of three sentences that describe what it is that I'm doing.

play11:43

Add some constraints and now I'm ready to start adding in my code

play11:48

and you'll actually notice that great italicized text there.

play11:51

That's GitHub Copilot and action.

play11:53

It's making those suggestions.

play11:55

I actually haven't done anything yet and it's already

play11:59

starting to make code suggestions.

play12:02

I'm going to accept what this is going to give me by hitting tab,

play12:05

but I'm going to be a little bit more specific.

play12:08

So I want a speaker.

play12:09

Speaker should have name,

play12:13

email and LinkedIn like that.

play12:17

And let me just give it one quick extra return here.

play12:20

And let's see, I'm speaker.

play12:23

What's let's do this.

play12:24

Let me be more specific here.

play12:26

I'm learning from my self here.

play12:29

Create a speaker model

play12:33

with name, email and LinkedIn.

play12:37

There we go

play12:39

in this moment. Perfect.

play12:41

So what you notice there is previously was saying, should I

play12:46

now I'm being a little bit more specific about what it is that I want.

play12:49

I'm being flexible, rolling with it.

play12:51

I go back, I tell it create and now it's doing its thing.

play12:55

And so now if I had Tab, now

play12:57

it's got all of that information that I specified.

play13:01

So it's got my name, it's got my email and it's got that linked in URL.

play13:06

You might also notice it's including a little thing that I didn't ask for.

play13:10

It's including that override of the SDR function.

play13:13

This is something that's very common in Django and it just automatically added

play13:18

that in I could, you know, remove that if I want to, but I actually like that.

play13:23

That's a very common convention inside of Django.

play13:27

So with that speaker created

play13:29

the next thing that I want to do is let's create a track.

play13:33

So let's say create track

play13:36

with name and description.

play13:39

Sure. And maybe an abbreviation

play13:43

like that.

play13:44

There we go.

play13:46

Give that a moment and that will go ahead and set that up for me.

play13:51

Again, it's reading that comment and then adding the rest down below.

play13:55

Now, as I've highlighted, be specific.

play13:59

You'll notice that I said abbreviation.

play14:02

And so it said, All right, fine, I'll give you an abbreviation.

play14:04

Abbreviation is probably three characters.

play14:07

And that's what I'm seeing right here.

play14:08

Is it set up that abbreviation with that max length of three characters.

play14:13

But chances are, if I'm going to be looking for

play14:16

an abbreviation, I want something that's a little bit more specific.

play14:20

So I want this abbreviation to be specifically

play14:23

three letters and maybe three uppercase letters.

play14:26

So let's go back and just make that modification.

play14:29

I'm just going to get rid of that and I'm just going to say abbreviation

play14:33

should be three uppercase letters just like that.

play14:39

And now it will go ahead and build that out

play14:44

with that regular expression. Now,

play14:48

I don't know about you, but at least for me,

play14:50

I can read regular expressions and something like this

play14:53

I could certainly have typed out off the top of my head,

play14:56

although if I'm being perfectly honest, I do sometimes forget which is which.

play15:01

If it's the carried character,

play15:02

the dollar sign to pin to the beginning, pin to the end.

play15:05

So it's nice not to have to always go look those types of things up.

play15:09

So I was able to just describe this is what I want.

play15:12

It generated that regular expression.

play15:14

So I was flexible there, I was specific and it gave me back that that value there.

play15:20

Pretty cool.

play15:22

Last but not least, let's

play15:24

go ahead and create that torque.

play15:28

It's already adding in that comment.

play15:30

I'm going to go ahead and hit tab to accept this.

play15:32

Let me read a title description Speaker Track and date.

play15:36

Perfect. Let's go ahead and accept that

play15:41

and give it 2 seconds here.

play15:44

Perfect.

play15:45

And now we'll go ahead and put all of that in.

play15:49

So now it's got my title, it's got my description,

play15:51

it's got my speaker, it's got my track, it's got my date,

play15:54

and then a little bit of extra stuff down at the very bottom here.

play15:58

And I'm going to kind of leave that the way that it is for the moment.

play16:01

But the big thing that I want to point out is that unfortunately,

play16:06

it didn't quite pick up what it was that I was looking for, that if we remember

play16:12

from the very beginning here, let me scroll all the way back up.

play16:16

I specified in my little header

play16:18

that I wanted to make sure

play16:21

that a parent can't be deleted if there are children.

play16:25

Unfortunately, this didn't pick up on that.

play16:28

Maybe I just wasn't clear enough.

play16:29

Maybe I needed to use the specific vocabulary.

play16:34

Who knows?

play16:35

But that's all right.

play16:36

I could go back and I can modify it. Now.

play16:38

Before I do that, though, I actually want to get in to the next set of tips here.

play16:44

And the first is to name things properly.

play16:47

I'm going to bounce back and forth a little bit

play16:49

between the slide in this code here to kind of make some points.

play16:52

And the first is right here on Linked In,

play16:57

what you're going to notice is that when I added in that comment

play17:01

of create speaker model with name, email

play17:04

and LinkedIn that it automatically set up that LinkedIn

play17:09

as a new URL field, which is probably exactly what I wanted.

play17:14

How did it do that?

play17:16

Well, it did that because it's actually reading my variable name here.

play17:22

So it sees LinkedIn.

play17:24

It goes, okay, you probably want a URL and it will automatically put that in there.

play17:29

Which also then means that if I'm looking for good suggestions from GitHub Copilot

play17:35

that I want to make sure that my variable names are also good.

play17:40

One of my pet peeves encoding

play17:42

is abbreviations is single letter variable names.

play17:47

Because when I go back and I take a look at that code,

play17:51

it's not always clear to me what that code is supposed to be doing.

play17:56

And when it comes to abbreviations, sure, maybe we've got documented somewhere

play18:00

what abbreviations mean and so forth, but that's now institutional knowledge.

play18:05

That's something that my developers would need to learn.

play18:09

And the exact same thing would then wind up holding true with GitHub Copilot

play18:13

is it's not going to have that same experience as my developers.

play18:17

It's not always going to be able to pick up what those abbreviations mean.

play18:22

So if I've got good code, if I'm spelling these things out,

play18:26

I'm not only making it clear to my other developers, but

play18:29

I'm also making it clear to get a Copilot, which going back to that analogy of

play18:36

it being in a pair programmer, I kind of want to treat it as such.

play18:40

I want to make sure that I'm being clear to it.

play18:42

So because of the fact that I use LinkedIn here,

play18:45

it was able to go, okay, I know what you're doing.

play18:49

And it added in that that you are our field.

play18:53

Now keeping along with that, using good code.

play18:57

Let's come back down here

play18:59

to where that little problem is that I highlighted a moment ago.

play19:02

Right now the speaker and track are both set to cascade.

play19:08

If you're not already familiar with Django, what that means

play19:13

is that if I had a speaker and they had talks, I delete that speaker.

play19:17

All of those talks are now going to go away.

play19:19

That was not what I was looking for.

play19:22

So fortunately I can change this, may actually

play19:24

just get rid of that full track line right there.

play19:29

And let me do sound effects help, by the way.

play19:34

There we go. Perfect.

play19:36

Let me go ahead and say protect.

play19:39

So by saying protect what that now means

play19:42

is it's going to prevent a talk

play19:47

from or a speaker from being deleted if they have talks.

play19:51

That was the behavior that I was looking for.

play19:56

Now that I've done that, let me have return here.

play19:59

And what you're going to notice

play20:01

is that with track, it's going to follow that pattern.

play20:04

So it's going to learn by watching me.

play20:08

So it's picking up that context.

play20:09

Following along, one of the most common questions that I get asked

play20:14

is, Hey, we have certain conventions, that we have certain conventions

play20:19

for naming our variables or whatever it is that it might be.

play20:22

How do I make sure that GitHub Copilot will follow with this?

play20:26

The simplest way that you can do that is show it some code

play20:30

that's written, the way that you're looking for it to be written,

play20:33

and it will then follow along with you,

play20:38

kind of reads perfectly in to following best practices.

play20:43

GitHub Copilot is going to what you're doing

play20:45

and it will follow right along with that.

play20:48

Last but not least is like I highlighted up at the the towards

play20:52

the middle I guess is to make sure that you are opening up relevant files.

play20:57

So if I'm adding in code, not quite getting what I'm looking for,

play21:00

make sure that I've got my relevant files open.

play21:04

All right.

play21:04

Let's some keep building here and I want to create some views

play21:10

to display the data that I've now got for my website.

play21:14

So I need to import it a little, little bit of code here.

play21:18

So GitHub Copilot sees that I'm doing Django automatically.

play21:22

Pull that in.

play21:23

Let me just go ahead and hit tab hit a couple of carriage returns

play21:27

and now notice this next line here.

play21:30

The next line that it automatically is adding in

play21:33

is it's importing in my talk, my track, my speaker.

play21:37

It's doing that because it's now

play21:40

seeing my model's tab open.

play21:43

It knows what's inside of there and we'll automatically import those in.

play21:48

Let me go ahead and now start creating

play21:52

views for my let's aim for my talk.

play21:56

So let's go ahead and say create a list view

play22:02

for my talks to become

play22:06

and it will go ahead and set that up for me.

play22:11

Now, what this is doing in Python, Django,

play22:14

is this is specifying that the model that we're going to be working with

play22:16

is talk that when I actually go to display the data, it's

play22:21

going to have an object name of talks in the HTML

play22:25

template that it's going to use is going to be that talk list.

play22:29

Now, this is probably one of my favorite

play22:33

little things about Django is because this is a framework

play22:38

that is built to very quickly allow me to front ends for databases.

play22:44

It includes a lot of pre baked code

play22:47

for me that I don't have to go in and create.

play22:51

But as with most frameworks, I would say

play22:54

it's not always going to be the most performant right out of the box

play22:58

that by default what this is going to do is it's going to do a thing

play23:02

called lazy loading, meaning that when it goes to display

play23:05

every single talk, it's going to have to go back and go get the speaker.

play23:09

It's going to have to go back and get the track to display that out.

play23:12

Obviously, I don't want all of those roundtrips to my database.

play23:16

That's all right.

play23:17

I can avoid that.

play23:18

That what I can do is I can add in a comment

play23:22

describing what I want so I can say, Hey,

play23:25

select the name and ID for the track.

play23:31

Oops,

play23:34

I have a great memory.

play23:34

It's just really short.

play23:35

When I said tracked, I say name. I did.

play23:37

Let me go.

play23:38

Never do it yourself.

play23:39

And the name and ID for speaker.

play23:45

And now when I add that in

play23:48

let's say pre-select.

play23:52

There

play23:54

we go.

play23:55

Boop.

play23:58

There we go.

play23:59

And so now it's going to go ahead and it's going to do the select

play24:02

related for my track and for my speaker.

play24:06

And unfortunately pick up on that name or on that ID that is not a problem.

play24:11

I can just go back and again, kind of just roll with it here, be flexible.

play24:16

So I'm going to say trac,

play24:19

underscore, underscore ID, and now

play24:23

I'll say track name and this should pick up here.

play24:26

Let me just get rid of that reproof.

play24:30

There we go.

play24:32

And now it's picked up on that ID

play24:34

and it's picked up on that name for me.

play24:38

So again, be flexible with it, kind of roll with it and it will

play24:42

then help you out.

play24:44

Now, that's for all of my talks.

play24:46

Let me go ahead and create one for a detail for my talks.

play24:51

Copilot knows where I'm going, so I'll go ahead and hit tab.

play24:54

Except that

play25:01

there we go and accept that.

play25:03

And just like we saw before, because of the fact that I did

play25:07

that pre-select previously and automatically adds that in.

play25:12

So it's following along based on what it is that I am doing here.

play25:18

And that's going to be one of the big takeaways again,

play25:21

that I want to make sure that I highlight it will follow along with you.

play25:25

Okay.

play25:26

One last little thing to highlight here

play25:30

is let's go back over here to

play25:34

my talk and let's add in one last little thing.

play25:38

So up until now, what I've been focused in on

play25:42

is trying to kind of get the most out of GitHub

play25:45

Copilot to build out my model, to build out my view.

play25:49

And now I want to see how I could, you know, take this little talk here.

play25:55

Maybe to one last little level add

play25:58

in a dynamic field for my code.

play26:01

So every talk should have a code.

play26:03

That should be the in my case, my track,

play26:07

maybe a level and then the ID for the talk.

play26:11

And I'm going to build that out here.

play26:13

So let me first up add in a level, add a level field

play26:20

with value range

play26:23

from 1 to 3,

play26:28

just like that.

play26:30

And so it's got my men and my max of 1 to 3.

play26:33

That's perfect.

play26:34

And now let's go ahead and create a

play26:38

computed field called code.

play26:43

Code should have

play26:49

the track a dash,

play26:52

the level and two digits from the ID.

play26:58

Now, if you remember previously, one of the things that we talked about

play27:03

is give it an example so it might be able to pick this up from.

play27:07

But I don't know about you, but at least for me, I always like to see examples.

play27:11

Examples are a great way to kind of describe and also to document.

play27:16

So I put it in one more comment and it automatically kind of picked this up.

play27:21

So it automatically added an example of in this case P by t-101

play27:26

That's I'll accept that and then go ahead and hit return.

play27:30

It's going to add in with my property hit return

play27:33

and then now it will build out that property for me.

play27:36

So now it's got that abbreviation,

play27:38

it's got that level and it's got this idea of zero two.

play27:42

Now in this case it already did that by utilizing F string.

play27:46

Again, going back to specificity, if I want to make sure

play27:49

that it does it a particular way, just ask it to do it that way.

play27:53

So if it didn't give me that level of specificity, sorry,

play27:56

if it didn't give me that F string,

play27:58

then I can go back and be a little bit more specific.

play28:02

So I kind of want to go back here

play28:05

and flow back through all of this one last time.

play28:08

Number one, describe the goal at the top of the file.

play28:11

Add in a little quick comment.

play28:13

Couple lines, three lines, four lines.

play28:14

What you're doing, how it is that you're going to do it, Any relevant

play28:18

information that you think that GitHub Copilot might need?

play28:21

Number two, be flexible.

play28:24

We saw this as we went through our demos here.

play28:26

That GitHub Copilot is probabilistic that it's not always going

play28:31

to give you back the exact same values in the exact same situation.

play28:35

Things will sometimes change and we want to be flexible with that.

play28:39

Number three is provide examples and this can be really helpful to GitHub

play28:45

Copilot to be able to understand what it is that I'm trying to accomplish.

play28:50

As a side note, one question that I very frequently get asked

play28:54

is about those comments. Should we remove them?

play28:57

Should we add them?

play28:58

My personal opinion is leave them behind.

play29:02

And the reason for that is because they are artifacts.

play29:05

It was how my code was generated and I want to be able to see this.

play29:09

And if we're later going to go back and do any fine tuning on that model,

play29:13

having that text describing

play29:14

what it was that was built is going to be really helpful.

play29:18

Always remember, and this is kind of a universal truth,

play29:21

I think that good code begets good code,

play29:24

name things properly follow best practices.

play29:27

When you do that GitHub, Wolf or GitHub Copilot will follow along with that.

play29:33

And then finally, the most important part, probably the theme of this entire

play29:39

presentation is above all else, go with the flow.

play29:46

With that, I want to thank you for your time.

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
GitHub CopilotCoding ToolsAI AssistantDeveloper TipsCode CompletionIDE IntegrationContext UnderstandingCode EfficiencyDjango FrameworkBest Practices