I created a gacha API in 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

To avoid being left behind in this age of advanced AI, GitHub Copilot I tried coding using

How to install

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

Instructions on how to install plugins for each editorthis linkcan be found at

A subscription is required, sothis linkyou need to register via

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

, just like with regular code completionCtrl+Spacecan be displayed using shortcut keys such as

Usage example

PHP 8.2 with Laravel I will be using

my IDE IntelliJ IDEA Ultimate For this project, I used

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

  • rand(0,$sumWeight)If you use$sumWeightis 10000, 0 will also be randomly selected within the range of 0 to 10000.
  • randinstead ofmt_randI want to use
  • 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 code to retrieve data from a model called "Gacha Master," the system// Error if data does not exist in Gacha Master"will automatically add the comment "

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,605
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Shinta

I'm a Yokohama-born, game engineer and a dedicated engineer!