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

GitHub
1 Dec 202329:53

Summary

TLDRクリス托ファー・ハリソン氏は、GitHub Copilotの使い方を紹介し、開発者向けにコード補完ツールの活用方法を解説します。CopilotはOpenAIを活用し、コードとコメントをもとに提案を生成しますが、正確な動作にはプロンプトの工夫が必要です。ファイル全体や開いているタブをコンテキストとして使用し、意図や具体性を明確にすることで、より良い提案を得られます。デモを交えながら、モデルやビューの作成方法を学ぶことができます。

Takeaways

  • 🔧 GitHub Copilotはツールであることを忘れがちだが、コード補完を始めとした一連のツールとして機能します。
  • 🛠️ GitHub Copilotはコードやコメントをもとに提案を生成し、開発者がIDEでそれらを受け入れるか拒否するか選択できます。
  • 🌐 コンテキストの理解が重要で、現在作業しているファイル全体や開いているタブがGitHub Copilotに送信されます。
  • 📚 タブの順序によってGitHub Copilotは関連性の高いファイルに焦点を当て、最も関係のあるものを優先的に解析します。
  • 💡 プロンプトの工夫(prompt crafting)は、GitHub Copilotが最も効果を発揮するようにコードやコメントを記述する技術です。
  • 📝 コードのコメントや変数名は簡潔明瞭で、専門用語や省略形は避けるべきです。
  • 📉 具体性は重要で、具体的な要件を満たすためには、詳細なプロンプトを提供することが有効です。
  • 🔄 GitHub Copilotは確率的であるため、同じ状況でも異なる提案が返される可能性があることを理解し、柔軟に対応する必要があります。
  • 📖 良いコードは良いコードを生み出し、変数名やコードの命名規則に注意することでGitHub Copilotがより良い提案を生成するようになります。
  • 📝 コメントはコード生成のアーティファクトとして残しておくことが役立つため、削除するべきではないと考えられています。
  • 🌟 最も重要なのは、GitHub Copilotと一緒の流れに沿って柔軟に対応することで、最大限に活用することができる点です。

Q & A

  • GitHub Copilotはどのようなツールですか?

    -GitHub Copilotはコード補完を含むツールのセットで、エディター内でコメントやコードを追加する際に、その文脈に基づいて提案を生成するツールです。

  • GitHub Copilotがどのように動作するのか教えてください。

    -GitHub Copilotは、入力されたコードやコメントを受け取り、OpenAIの技術を利用してその文脈を理解し、提案を生成してIDEに戻します。

  • GitHub Copilotが送信するコンテキストには何が含まれますか?

    -現在作業しているファイル全体や、IDE内で開かれているタブの情報が含まれます。特に、現在作業しているタブに近いタブの情報に重きが置かれます。

  • プロンプトクラフトとは何を意味していますか?

    -プロンプトクラフトは、コメントやコードをどのように入力するか、どのようなフレーズを用いてGitHub Copilotから最大限の利益を得る方法を意味します。

  • なぜコメントをコードの先頭に記述することが重要なのですか?

    -コメントをコードの先頭に記述することで、GitHub Copilotはコードの目的や意図を理解しやすくなり、より適切なコード提案を生成することができます。

  • なぜGitHub Copilotは確率的な動作をするのでしょうか?

    -GitHub Copilotは機械学習に基づくため、同じ状況で同じ入力をしても異なる提案が返されることがあります。これは確率的な動作と呼ばれています。

  • GitHub Copilotが提案を改善するために使用する例を提供することの重要性は?

    -例を提供することで、GitHub Copilotは開発者が何を目指しているのかをより明確に理解し、より正確なコード提案を生成することができます。

  • なぜ変数名やコードの命名規則が重要なのですか?

    -適切な変数名やコードの命名規則を用いることで、コードが明確になり、GitHub Copilotもそのコンテキストを理解しやすくなります。

  • GitHub Copilotが生成したコードをどのように微調整するべきですか?

    -必要に応じて、GitHub Copilotが生成したコードを手動で調整し、特定の要件に合わせて修正することで、より適切な結果を得ることができます。

  • GitHub Copilotの使用でコメントを残すべきか、それとも削除すべきですか?

    -コメントはコード生成の過程を記録しており、後のコードの微調整や理解に役立つため、残した方がよいとされています。

  • どのようにGitHub Copilotと協調してコードをより良くすることができるか?

    -正確なプロンプトを提供し、良いコードの命名規則に従い、GitHub Copilotが生成するコードに対して柔軟かつ具体的なフィードバックを提供することで、より良いコードを構築できます。

Outlines

00:00

😀 GitHub Copilotの紹介と使い方

クリス托ファー・ハリソン氏はGitHubのシニアデベロッパーアドボケイトとして、GitHub Copilotの紹介を行います。Copilotは開発者向けのツールで、コード補完を提供します。コードやコメントを入力すると、そのコンテキストに基づいて提案が生成されます。CopilotはOpenAIの技術を利用しており、開発者がIDEで作業している間にリアルタイムのサポートを提供します。また、ファイル全体や開いているタブの情報からコンテキストを取得し、最も関連性の高いものを優先的に扱うことが強調されています。

05:04

🔍 コンテキストとプロンプトの重要性

プロンプトの工夫(prompt crafting)について説明し、GitHub Copilotを最大限に活用する方法を提案します。コンテキスト、意図、明確性、そして具体性という4つの要素が重要です。コメントやコードを記述する際には、これらの要素を念頭に置いて明確で具体的に情報を提供することで、より良い提案を得ることができます。また、開発者が自然な作業パターンでファイルを開くことで、Copilotは関連性の高いファイルに焦点を当てることができます。

10:04

🛠️ GitHub Copilotの活用テクニック

GitHub Copilotを活用するための具体的なテクニックについて説明します。コードの例を提供することで、Copilotが求めているものを見つけやすくなります。また、モデルの構築やビューの作成など、実際のコーディングデモを通じて、Copilotがどのように開発者の意図に従ってコードを生成するかを示します。コメントを通じて意図を明確にし、Copilotがより適切なコードを提案できるようにすることがポイントです。

15:06

🔧 命名とコードの質の重要性

良いコードの命名とコードの質がGitHub Copilotの提案に与える影響について説明します。Copilotはコードの命名規則やベストプラクティスに従うことで、より適切な提案を行います。開発者は一貫性のある命名規則を用いることで、Copilotがより理解しやすくなり、効率的なコード生成が促進されます。また、開発者が行う変更に基づいてCopilotが学習し、より良いコードの生成を続けることが強調されています。

20:08

📝 コメントの役割とコード生成

コメントを通じてGitHub Copilotに意図を伝え、コード生成を促進する方法について詳述します。コメントはコード生成の過程で非常に重要で、Copilotはコメントを読み取り、それに応じたコードを提案します。また、開発者が行う変更に対してCopilotは柔軟であり、必要に応じてコメントを調整することで、より適切なコード生成が可能になります。

25:12

🌟 GitHub Copilotの学習能力と柔軟性

GitHub Copilotの学習能力と柔軟性を強調し、開発者がCopilotと協調して最良の結果を得る方法についてまとめます。Copilotは開発者の行動に基づいて学習し、それに応じてコードの生成を調整します。また、開発者はCopilotが提供するコードに対して柔軟に対応し、必要に応じて調整することで、より良い結果を得ることができます。最後に、コメントを残しておくことで、コード生成の過程を記録し、後で参照することが可能になる点も紹介されています。

Mindmap

Keywords

💡GitHub Copilot

GitHub Copilotは、コード補完を支援するツールです。開発者がコメントやコードを入力する際に、その文脈に基づいて提案を生成し、開発者の生産性を高めることを目的としています。このツールは、開発環境に統合されており、開発者がIDEで作業している間も動作します。ビデオでは、このツールの使い方や、その動作原理について説明しています。

💡コード補完

コード補完とは、開発者がコードを書く際に、開発環境が自動的にコードの続きを提案する機能です。GitHub Copilotでは、開発者が入力するコードやコメントに基づいて、その場で最も適切なコードを推測して提案します。ビデオでは、コード補完がどのようにして開発者の作業効率を高めるかが説明されています。

💡コンテキスト

コンテキストとは、GitHub Copilotがコードを提案する際に使用する情報のことです。これは、開発者が現在作業しているファイル全体や、開いているタブの内容などから得られます。ビデオでは、コンテキストが正確なコード提案にどのように重要であるかが強調されています。

💡プロンプトクラフト

プロンプトクラフトは、開発者がGitHub Copilotに対して、より正確なコード提案を得るためにコメントやコードをどのように記述するかの技術です。ビデオでは、プロンプトを明確にすることで、ツールがより適切な提案を生成するように導く方法が紹介されています。

💡インテント

インテントとは、開発者がGitHub Copilotに対して伝えたい具体的な意図や目的のことです。ビデオでは、インテントを明確にすることで、ツールがより適切なコードを提案するように促す方法が説明されています。

💡クラリティ

クラリティとは、コメントやコードでの意図の表現の明確さを指します。ビデオでは、開発者がコードを記述する際に、簡潔で明確な言葉を使うことの重要性が強調されています。これは、GitHub Copilotが正確な提案を生成するのを助けるためです。

💡具体性

具体性とは、開発者がGitHub Copilotに対して求めるコードの詳細を具体的に伝えることを指します。ビデオでは、具体性を高めることで、ツールがより正確なコードを提案することができると示されています。

💡Django

DjangoはPythonで書かれた、高レベルのWebフレームワークです。ビデオでは、Djangoを使用してウェブアプリケーションを作成する際に、GitHub Copilotがどのように役立つかが説明されています。また、Djangoのモデルやビューを作成する例が示されています。

💡柔軟性

柔軟性とは、開発者がGitHub Copilotの提案に対して適応し、必要に応じてコメントやコードを再構成する能力を指します。ビデオでは、ツールの提案は確率的であり、同じ状況でも異なる結果が返されることがあるため、開発者は柔軟に対応する必要があると説明されています。

💡良いコード

良いコードとは、読みやすく、メンテナンスしやすいコードを指します。ビデオでは、良いコードの書き方、例えば適切な変数名の使用や、一般的なコーディング規約への従順が、GitHub Copilotがより良い提案を生成するのを助ける方法として紹介されています。

Highlights

Christopher HarrisonはGitHubのシニアデベロッパーアドボケイトであり、GitHub Copilotの活用方法について話します。

ツールは時に魔法のように感じるが、最終的にはIDEと同じくツールであることを忘れずに、最大限に活用する方法を見極めましょう。

GitHub Copilotはコード補完を中心としたツールで、コメントやコードを入力すると提案を生成します。

GitHub CopilotはコードやコメントのコンテキストをOpenAIモデルに送信し、提案を生成して返送します。

コンテキストは現在作業しているファイル全体や、IDE内の開いているタブに基づいて送信されます。

タブの順序によってGitHub Copilotは最も関連性の高いファイルに焦点を当て、提案を生成します。

プロンプトクラフトは、コメントやコードの入力方法を学ぶことでGitHub Copilotを最大限に活用できる技術です。

コンテキスト、意図、明確性、特定性を含んだプロンプトを用いることで、GitHub Copilotはより正確な提案を生成します。

開発者が最初にGitHub Copilotを試した際の大きな失敗は、十分なプロンプトを提供しないことで、期待する大きな結果を得られなかったことです。

ファイルの先頭に目標を記述することで、GitHub Copilotが現在の作業内容を理解しやすくなります。

ツールは確率的であるため、同じ状況で異なる提案が返されることがあることを理解し、フレキシブルに対応しましょう。

具体例を提供することで、GitHub Copilotが求めている結果をより明確に伝えることができます。

Djangoフレームワークを使用してウェブサイトのモデルを作成するデモンストレーションを行いました。

適切な変数名を使用することで、GitHub Copilotがより良い提案を生成するのに役立ちます。

GitHub Copilotは見たコードに基づいて学習し、ユーザーが従うべき命名規則やベストプラクティスに従うようになります。

関連するファイルを開くことで、GitHub Copilotはより適切な提案を生成することができます。

GitHub Copilotはユーザーの行動に従い、コードの生成を支援することで学習を進めます。

最終的に、GitHub Copilotを最大限に活用するためには、流れに従い柔軟に対応することが重要です。

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)

相关标签
GitHubCopilot開発効率化ヒントテクニックコード補完IDEプログラミングツールチュートリアル
您是否需要英文摘要?