I created a gacha API using PHP with GitHub Copilot!

nice to meet you

Thank you for opening the article

This is Shinta from the System Development Department

We've been living in an incredibly interesting era recently, with the development of AI technologies such as ChatGPT

In order to avoid being left behind in this age of AI, I tried coding using GitHub Copilot

How to install

GitHub Copilot can be used by installing a plugin for editors such as Visual Studio Code and JetBrains IDE

You can find instructions on how to install plugins for each editor this link

A subscription is required, so you will need to register using this link

Note! ! !

If you leave the personal plan at its default settings, your source code will be used to learn Copilot

You can change it so that it doesn't learn, so be sure to change it. If you're using the Business plan, it's probably set to not learn by default

How to use

GitHub Copilot works by showing code completion suggestions

While you are typing, code completion suggestions will be displayed in grey text, and you can select them to complete the code

can be displayed using shortcut keys such as Ctrl+Space , just like regular code completion

Usage example

I use laravel with PHP8.2

The IDE used this time was IntelliJ IDEA Ultimate

Try to create an API that executes gacha using only comments as much as possible

I entered almost only the comment part, and occasionally the variable names were incorrect, so I only corrected those

When you write a comment, suggestions will appear as shown in the area surrounded by a red frame. If there are no problems, press Enter to complete the comment

The lotteryItem function does not exist at this point so we need to create it

Create lotteryItem

As soon as I entered the function name lotteryItem, a seemingly perfect source code appeared as a candidate

If you look closely, it's different from the process you actually want to create

  • If you use rand(0, $sumWeight) $sumWeight is 10000, 0 will be drawn between 0 and 10000.
  • I want to use mt_rand instead of rand
  • I personally find addition easier to see than subtraction, so I want to change it
  • The argument is not a Collection
  • The comment is a bit strange

Try to fix it

Operation check results

I think there's probably no problem!

My impressions after using it

In the beginning, there isn't much support

While I was creating the lottery function, it only completed a few lines. I think it probably didn't understand what I wanted to create because there wasn't much existing code

Suddenly, at a certain point, they start to complement each other intensely

Once I created the lotteryItem function, I had a source code that looked perfect. There were some errors, but they were all within the realm of possibility of being fixed

I think the more information you provide, the more accurate the completion will be

If the completion candidate is different from what you intended, multiple candidates can be displayed

It will give you several options so you can choose the one that best suits your needs

Once the process you want to create becomes clear, it will automatically write comments

For example, if you write a process to retrieve data from a model called Gacha Master, the next line will output a comment saying " Error" if the Gacha Master data does not exist

I'm very happy because it simply reduces the amount of typing

Future challenges

Learn how to write comments that are clearly understood by Copilot

If your comment is too general, the completion suggestions may not be what you intended

You won't know about this until you actually try it out, so I think it's best to try it out and make adjustments as necessary

Depending on the processing, incorrect code may be completed

For example, when creating a login process, which is likely to have many examples, the completion results are quite accurate, but the accuracy is a little low for a process like this gacha. (I think)

It will return source code that looks like it, so be careful as using it as is may cause bugs

It is better not to use it unless you can write it yourself

There is a good chance that a long, inappropriate code will be returned. If you don't notice it at first glance and think it's OK, it could cause a disaster later on

It can be especially difficult for people with little programming experience to judge how much they can trust, so be sure to check that the processing is actually correct!

summary

GitHub Copilot is very useful. If you are clear about what you want to create, you can leave comments and have the source written in Copilot, which greatly improves work efficiency

However, relying too much on suggestions can lead to not noticing bugs, or following them too much can prevent you from developing the ability to think for yourself and implement things

As the name suggests, the Copilot is an assistant co-pilot, so it's important that you write the code as the main pilot

Have a nice flight!

If you found this article helpful , please give it a like!
11
Loading...
11 votes, average: 1.00 / 111
2,492
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Shinta

I'm a game engineer born in Yokohama, and I'm a gutsy engineer!