代码看不懂?GitHub Copilot帮你翻译和解读

GitHub Copilot是一款基于人工智能技术的代码生成工具,帮助开发者能快速的编写重复性较高的代码,这种同类工具也是比较多的,比如国外比较流行的TabNine、Kite、国内的阿里云Cosy等,详细介绍欢迎大家阅读文章AI写代码工具哪款是你的菜?

代码看不懂?GitHub Copilot帮你翻译和解读

今天要为大家介绍的是GitHub Copilot Labs的VSCode插件,Labs插件是Copilot机器学习实验应用的试验场,用来改善开发者的编码体验,目前它能帮助程序员解读看不懂的代码片段,或者将某种语言的代码片段翻译为另一种开发语言,比如将Java代码翻译为Python代码,下面我们一起来了解一下。

安装插件

GitHub Copilot Labs的插件安装比较简单,与GitHub Copilot插件雷同,只需到VSCode插件市场搜索并安装GitHub Copilot Labs,然后登录GitHub账号进行授权即可。(前提是需要获得GitHub Copilot的邀测资格)

代码看不懂?GitHub Copilot帮你翻译和解读

安装过程我就不做过多介绍了,下面我们来一起看一下它能帮助我们做什么吧。

代码解读

作为程序员,大家经常会遇到看不懂的代码,有可能是遇到新的技术栈,有可能是刚接触编程的小白,也可能是需要阅读/维护别人编码的代码,这种时候大家很可能需要将代码运行一遍,亦或是直接询问朋友/同事。为了解决这个问题,GitHub Copilot Labs为我们提供了代码解读功能。

代码看不懂?GitHub Copilot帮你翻译和解读

我们只需用鼠标框选需要解读的代码片段,GitHub Copilot Labs会同步在左侧栏中展示款选的代码片段,并选择“Explain code”,然后点击“Ask Copilot”,即可为我们生成代码片段的详细解释。我们来看看Copilot对上图中的代码是如何解读的:

/* Here is the explanation for the code above:

1. We create a BufferedReader object and pass an InputStreamReader object to it.

2. We call readLine() method of the BufferedReader object to get the line of text.

3. We use a StringBuilder object to build up the text line by line.

4. We loop until the line is null, which means the end of the file has been reached.

5. Finally, we print the text. */

上面的代码比较简单,Copilot解读起来还是比较容易的,我们再看看另一段代码。

代码看不懂?GitHub Copilot帮你翻译和解读

/* Here is the explanation for the code above:

1. First, we get the html data from the data.html file, which is located in the same directory with this class.

2. Then, we use Jsoup to parse the html code, and get the content from the id=”mainbar”

3. Then, we get the content from the href attribute of the html code */

我们再找一道Leetcode的算法题“寻找两个有序数组的中位数”。

代码看不懂?GitHub Copilot帮你翻译和解读

/* Here is the explanation for the code above:

1. Check if k is odd or even.

2. Calculate the index for the first element in the first array.

3. Calculate the index for the first element in the second array.

4. Calculate the index for the kth element in the first array.

5. Calculate the index for the kth element in the second array.

6. Compare the kth element in the first array and the kth element in the second array. 7. If the kth element in the first array is less than or equal to the kth element in the second array, then the kth element is in the first array.

8. If the kth element in the first array is greater than the kth element in the second array, then the kth element is in the second array.

9. Otherwise, the kth element is in the first array.

10. We need to do this step k times.

11. Return the kth element. */

生成的代码解读结果更多偏语法层面,语义层面的还是比较少的,当然,能做到这点也是不易,但是实用性稍微差了一点。

代码翻译

相信很多小伙伴都想象过,是否写完了一个语言的代码,能直接翻译成其他开发语言运行。比如我可能只学习了Java,但是老板要求我用C++语言实现需求;亦或是我可能用Java在后端实现了一个工具类,但是在前端也需要有一个同样功能的JavaScript工具类进行对应;这种时候如果能有一个代码翻译工具,直接将Java代码翻译为C++/JavaScript代码,那就方便很多了。为了解决这个问题,GitHub Copilot Labs为我们提供了代码翻译功能。

我们先用上面那道Leetcode算法题“寻找两个有序数组的中位数”试试。

代码看不懂?GitHub Copilot帮你翻译和解读

我们从图中可以看到,Copilot Labs支持的语言种类是非常丰富的,几乎是全语种。上图中我们将Java代码分别翻译为了C++、Python、JavaScript代码,翻译的效果还是很不错的。

我们再试试之前那段爬虫代码。

代码看不懂?GitHub Copilot帮你翻译和解读

虽然生成的网站、html解析元素不一致,但是提供了Python语言做类似html解析的代码示例,可以作为参考使用。

我们再试试用Python代码生成Java代码。

代码看不懂?GitHub Copilot帮你翻译和解读

我们可以看到虽然能生成大致的“形”,但是代码逻辑是不一致的,多次点击“Ask Copilot”会生成不同的代码片段,经过多次尝试,发现生成的代码片段一个不如一个,看来用动态类型的脚本语言去生成静态类型语言的代码时不太理想的。

总结

总体体验下来,GitHub Copilot Labs的功能更多偏研究尝鲜,还处于“玩”的层次,离实际应用还需要一些时间,但是我们也能看到AI已经在逐步改善编程体验,或许在不久的将来,我们所想的终将会实现。

来源:天天写Bug

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年2月27日
下一篇 2022年2月27日

相关推荐