使用Cocycles按功能查找JavaScript代码段

Cocycles is a new code search service that enables code to be found by functionality. In this article, Cocycles team member Jonathan explains the idea behind this approach to code searching.

Cocycles是一项新的代码搜索服务,可通过功能查找代码。 在本文中,Cocycles团队成员Jonathan解释了这种代码搜索方法背后的想法。



We’re all familiar with it: that moment when you’re forced to abandon your code editor, interrupt your workflow and open your browser to search for a code snippet or explanation of how to code something. It can be a time consuming and frustrating process.

我们都熟悉它:那一刻,当您*放弃代码编辑器,中断工作流程并打开浏览器以搜索代码片段或如何编写代码的说明时。 这可能是一个耗时且令人沮丧的过程。

For years, I’ve found myself mostly turning to familiar search engines like Google and Bing, submitting queries like “JavaScript parsing HTTP headers” and praying that someone, somewhere, has asked about this, and that someone else has provided a useful answer and even a working snippet.

多年以来,我发现自己主要转向熟悉的搜索引擎,例如Google和Bing,提交“ JavaScript解析HTTP标头”之类的查询,并祈祷某个地方有人问了这个问题,并且其他人已经提供了有用的答案,甚至是一个工作片段。

Google and Bing can understand English pretty well, but they can’t read code. Sometimes luck’s on our side, and sometimes it’s not.

Google和Bing可以很好地理解英语,但是他们看不懂代码。 有时候运气就在我们这边,有时候却不是。

However, it doesn’t have to be like this. In April 2016, GitHub reported that it holds over 35 million repositories. That’s a whole lot of code lines! I’m willing to bet that most of what developers search for every single day already lies somewhere around the open-source web. Up till now, the problem has been that we can’t easily find it. This is why we built Cocycles, a search engine for code.

但是,不必一定是这样。 2016年4月,GitHub 报告称其拥有超过3500万个存储库。 这是很多代码行! 我敢打赌,开发人员每天搜索的大多数内容已经位于开源网站周围。 到目前为止,问题在于我们无法轻松找到它。 这就是为什么我们构建了Cocycles ( 代码搜索引擎)的原因。

In this article, I’ll introduce this new tool, explaining how it works and how to use it.

在本文中,我将介绍这个新工具,并解释其工作方式和使用方法。

Cocycles:功能代码搜索引擎 (Cocycles: a Functional Code Search Engine)

使用Cocycles按功能查找JavaScript代码段

More than anything else, Cocycles is a method of organizing knowledge and making it accessible. Cocycles does something fairly easy to describe, but incredibly challenging to do: it aspires to be able to find every piece of code in the open-source domain, catalogue it, and make it easy to find and use. So, it needs to do three things:

最重要的是,Cocycles是一种组织知识并使之可访问的方法。 Cocycles所做的事情相当容易描述,但是却具有巨大的挑战:它渴望能够在开源域中找到每段代码,对其进行分类,并使其易于查找和使用。 因此,它需要做三件事:

  1. know what every piece of code actually does

    知道每段代码实际上是做什么的
  2. understand what is it you are actually looking for

    了解您实际上在寻找什么
  3. match the two, sit tight and wait for the “Thank you” flowers.

    匹配两个,坐紧,等待“谢谢”花。

In reality, this is an ongoing process, in which community cooperation is a major key.

实际上,这是一个持续的过程,社区合作是主要的关键。

Cocycles is a technology that learns to understand what each piece of code does. It looks not only at the code’s signature, text or documentation, but also — and primarily — at its functionality. Cocycles understands code and what it does. It’s actually able to tell which pieces of code are best for swapping two elements in a given array, or creating a random string, and is able to identify them and even understand the connections between different pieces.

Cocycles是一种学习了解每段代码的功能的技术。 它不仅查看代码的签名,文本或文档,而且(主要)查看其功能 。 Cocycles了解代码及其功能。 实际上,它能够分辨出哪段代码最适合交换给定数组中的两个元素或创建随机字符串,并且能够识别它们甚至理解不同段之间的联系。

It currently supports JavaScript, and additional languages are planned later in 2016.

它目前支持JavaScript,其他语言计划在2016年晚些时候推出。

搜索普通英语代码 (Searching for Code in Plain English)

Cocycles is able to understand English. It’s also designed to understand developers. To achieve this, Cocycles uses natural language processing algorithms to understand the description of the code you need and find what you’re looking for.

摩托车能够听懂英语。 它还旨在了解开发人员 。 为此,Cocycles使用自然语言处理算法来了解所需代码的描述并找到所需的内容。

So when searching via Cocycles, it’s best to describe what you need in the same way you’d search for it in a search engine like Google, Bing and others. Unlike these, however, Cocycles is able to understand code, and therefore will translate your query into the desired code functionality.

因此,通过Cocycles进行搜索时,最好以与在Google,Bing等搜索引擎中搜索所需的方式相同的方式描述您的需求。 但是,与这些不同,Cocycles能够理解代码,因此会将查询转换为所需的代码功能。

For example, if you need to swap two elements in a given array, you should simply search for “swap elements”. If you need to parse the headers of an HTTP request, search for “parse http headers”. And when looking for a function to create a random string, try searching for “create random str”.

例如,如果需要交换给定数组中的两个元素,则只需搜索“交换元素”。 如果您需要解析HTTP请求的标头,请搜索“解析http标头”。 并且在寻找创建随机字符串的函数时,请尝试搜索“ create random str”。

使用Cocycles按功能查找JavaScript代码段

Thanks to semantic capabilities, Cocycles is also able to understand that “String” is the same as “Str” and “Create” might mean “Generate”. Furthermore, you can try looking for something broader, such as “timer”, and find useful timer classes with different methods.

借助语义功能,Cocycles还能够理解“字符串”与“ Str”相同,而“创建”可能意味着“生成”。 此外,您可以尝试寻找更广泛的内容,例如“ timer”,并使用不同的方法找到有用的计时器类。

Another neat feature is called “Cocycles Immediate”. When Cocycles knows exactly what it is you’re looking for, it will provide an immediate description of the desired result with important key info — as seen in the image below, which shows a search for “angular isStr”:

另一个简洁的功能称为“立即Cocycles”。 当Cocycles确切知道您要查找的内容时,它将通过重要的键信息立即提供所需结果的描述-如下图所示,其中显示了对“ angular isStr”的搜索:

使用Cocycles按功能查找JavaScript代码段

学习代码的工作原理 (Learning How the Code Works)

When opening a result unit, Cocycles takes you in to an IDE-like interactive exploration mode. It allows you not only to view the full source implementation itself, but also interact with the code. Hovering over certain parts will highlight matching parts; clicking on a certain variable or function will move you to its definition; and soon it will be possible to jump between files and even entire projects. This takes Cocycles out of the world of “snippets” and into the world of learning, exploring and making code useful. This last view will adapt to fit various types of results. For example, if the selected result is a class, Cocycles will list its methods and will offer a quick browse between them.

打开结果单元时,Cocycles将带您进入类似于IDE的交互式探索模式。 它不仅使您可以查看完整的源代码实现本身,还可以与代码进行交互。 将鼠标悬停在某些部分上会突出显示匹配的部分; 单击某个变量或函数将使您转到其定义; 很快就有可能在文件甚至整个项目之间跳转。 这使Cocycles走出了“片段”的世界,进入了学习,探索并使代码有用的世界。 最后一个视图将适合各种类型的结果。 例如,如果选定的结果是一个类,那么Cocycles将列出其方法并提供它们之间的快速浏览。

When available, Cocycles will also provide the full original documentation for every code unit found. Even cooler, it will find real usage examples from which you can learn how to use the code you find and how other people did so. If needed, you can also view the code’s source in GitHub directly.

可用时,Cocycles还将为找到的每个代码单元提供完整的原始文档。 更酷的是,它将找到真实的用法示例,您可以从中学习如何使用找到的代码以及其他人是如何使用的。 如果需要,您还可以直接在GitHub中查看代码的源代码。

展望未来 (Looking Into the Future)

Cocycles isn’t perfect. The technological challenges are massive. It’s a process, and an ongoing endeavor to organize all the code in the world and make it accessible.

摩托车并不是完美的。 技术挑战是巨大的。 这是一个过程,也是一项持续的工作,目的是组织世界上的所有代码并使其可访问。

It’s being built with help from the community, and by advice from some of the web community’s leading lights. It’s free, and built to last.

它是在社区的帮助下以及Web社区中一些领先者的建议下构建的。 它是免费的,并且经久耐用。

We also understand that the future lies in providing even more value to people — by harnessing more of the potential hidden in the open-source world.

我们还理解,未来在于通过利用更多隐藏在开源世界中的潜力为人们提供更多价值。

For example, we should be able to identify in real time when a developer is making a mistake, or is writing less than optimal code. We need to be able to offer an instant solution based on open-source code. We’re currently working on something that will offer all of this and more. We have a newsletter you can subscribe to, and a Twitter feed, if you want to be notified of updates.

例如,我们应该能够实时识别开发人员犯错或编写的代码不够理想的时间。 我们需要能够提供基于开源代码的即时解决方案。 我们目前正在研究可以提供所有更多功能的产品。 如果您想收到更新通知,可以订阅我们的时事通讯和Twitter feed

It’s yours to use, and any kind of feedback, ideas or thoughts would be gratefully welcomed. Visit cocycles.com and tell us what you think!

它是您的使用,任何反馈,想法或想法都将受到欢迎。 访问cocycles.com ,告诉我们您的想法!

翻译自: https://www.sitepoint.com/find-javascript-code-snippets-by-functionality-with-cocycles/