×
Community Blog Copilot! Smart Code Recommendation!

Copilot! Smart Code Recommendation!

This short article discusses Copilot and some practices of Copilot's code recommendation ability.

By Qinqi

GitHub Copilot has attracted a lot of attention since its inception. It is needless to say that its code recommendation ability is amazing, but some subsequent problems have aroused heated discussions, including whether programmers will lose their jobs because of Copilot and the copyright of its recommended code. However, this article does not intend to discuss these points. This article is some practice of Copilot's code recommendation ability after I passed the trial application. I hope it can be helpful to you.

Capability Summary

I roughly classify GitHub Copilot's capabilities into three types: assisted programming, text-to-code transformation, and empirical inference.

Assisted Programming

Assisted programming predicts and recommends possible codes based on the context of user input to make programming efficient. Here are some examples. The first example is the writing and comparison of the most common React components:

  • When there is no context, Copilot can recommend an available React component code.

1

  • When declaring IProps, the recommended component code contains interface definition.

2

  • When introducing Dialog and Table components, here is the recommended code:

3

Summary

When there is no context, Copilot can also recommend a usable code, although it is possibly not what we want. When there is a context, the recommended code is more accurate. The richer the context, the more accurate the recommendation.

Text-to-Code Transformation

Text-to-code transformation refers to the automatic generation of object code based on a description. Here are some examples:

  • String Reverse

4

  • Browser Detection is a more difficult one.

5

Although it takes more time, Copilot still recommends a decent code, whether it is available or has not been verified yet.

(P.S. /*@cc_on!@*/ is a unique code below ie11. Please refer to this link for more information.)

  • Here is another common interview question: quick sort.

6

Summary

Some commonly used functions and algorithms can generate codes according to descriptions, but the accuracy of generated codes cannot be guaranteed. These codes most likely come from actual code fragments. Whether they are correct or not needs to be judged by developers. I suggest that the generated code should be for reference only. It should not be used directly in production.

Empirical Inference

Copilot can automatically find out some laws implicit in the code. It can learn personal coding habits and recommend personalized codes if it is used often. Here is an example:

7

Another more complicated example:

8

The ability is quite amazing. I guess the logic behind it is similar to code2vec, which recommends according to the similarity of text semantics.

Personal Advice

Copilot is useful as a coding aid, but here are some tips:

⚠️ Don't rely too much on the generated code since its accuracy is not guaranteed.

⚠️ Currently, there is no local version of Copilot. Thus, the Internet is a requisite. Pay attention to the code security risk.

⚠️ Think of it as a development aid (like code completion or code search) but not a tool for making a living.

0 1 0
Share on

Alibaba F(x) Team

66 posts | 3 followers

You may also like

Comments