使用BigQuery分析GitHub上的C#代码

来源:互联网 发布:淘宝双11后生意不好 编辑:程序博客网 时间:2024/04/30 01:29

一年多以前,Google 在GitHub中提供了BigQuery用于查询的GitHub上的开源代码(open source code on GitHub available for querying),如果这还不够,您可以免费每月运行1TB的查询!

所以在这篇文章中,我将要收集GitHub上的所有我们可以访问的C#源代码。非常简单,只包含C#,而且数据集已经收集完成(在BigQuery中,是按字节读取收费的),称为fh-bigquery:github_extracts.contents_net_cs 并具有

  • 5,885,933 不重复的“.cs”文件

  • 792,166,632 行代码 (LOC)

  • 37.17 GB (37,174,783,891 bytes) 数据

这是一套非常全面的C#源代码!


这篇文章的剩余部分将 尝试 回答以下问题:

  1. Tab或空格?

  2. regions:“应该被禁止”或“在某些情况下使用”?

  3. “K&R” or “Allman”, C#开发者喜欢把它们的大括号放在哪里?(译者注:关于‘K&R’ 与 ‘Allman’,请参考维基百科)

然后转到一些没有争议的C#主题:

  1. 哪些using 声明使用最广泛?

  2. 哪些NuGet软件包常常被包含在.NET项目中

  3. C#文件一般有多少行代码(LOC)?

  4. 使用最广泛的“Exception”是哪一个?

  5. 使用“async/await”?

  6. C#开发人员是否喜欢使用“var”关键字?

在我们最终看到Repository:

  1. 包含C#代码最流行的Repository是哪一些

  2. 在一个Repository中一般有多少个文件?

  3. 最受欢迎的C# Class 名称中哪些?

  4. “Foo.cs”,“Program.cs”或其它,最常见的文件是哪些?

如果您想为自己尝试查询(或发现我的错误),那么所有这些查询可以在github上面找到,链接地址。很有可能我的正则表达式遗漏了一些边缘案例,毕竟正则表达式:现在有两个问题:


Tab或空格?

在整个数据集中有5,885,933个文件,但是我们只包括一个从Tab或空格开始的10行以上的文件

TabsTab(百分比)空格空格(百分比)总计799,05517.15%3,859,52882.85%4,658,583

显然,C#开发人员(在GitHub上)喜欢空格超过Tab !(我认为一些这可以通过Visual Studio中的事实来解释:默认情况下使用“空格”)。

如果您想了解C#与其它编程语言的比较,请查看40万个GitHub资源库,10亿个文件,14太字节的代码:空格或制表符?。

regions:“应该被禁止”或“在某些情况下使用”?

事实证明,有一个令人印象深刻的712,498 C#文件(总共580万),至少包含一个#region语句(查询方法),只有超过12%。(我希望很多这些文件已被工具自动生成!)

“K&R” or “Allman” ,C#开发者喜欢把它们的大括号放在哪里?

C#开发人员绝大多数喜欢将打开大括号{放在当单独的行上(查询方法)

单独行同一行同一个(初始化实例)总计 (包括大括号)总计(全部代码)81,306,320 (67%)40,044,603 (33%)3,631,947 (2.99%)121,350,923 (15.32%)792,166,632

(“同一行初始化实例”包括代码new { Name = "", .. }new [] { 1, 2, 3.. }


哪些 using 声明 使用最广泛?

这是一个更实质的统计,C#代码中,using 声明 最广泛的语句是哪一些?

显示前10名结果(全部统计结果在这里查看):

using 声明总计using System.Collections.Generic;1,780,646using System;1,477,019using System.Linq;1,319,830using System.Text;902,165using System.Threading.Tasks;628,195using System.Runtime.InteropServices;431,867using System.IO;407,848using System.Runtime.CompilerServices;338,686using System.Collections;289,867using System.Reflection;218,369

但是,当您在Visual Studio中添加新文件时,默认情况下包含前5个,许多人不会删除它们。默认情况下,“AssemblyInfo.cs”中包含的“System.Runtime.InteropServices”和“System.Runtime.CompilerServices”也是一样。

所以如果我们考虑到这一点,调整一下统计规则,前10名的结果是:

using 声明总计using System.IO;407,848using System.Collections;289,867using System.Reflection;218,369using System.Diagnostics;201,341using System.Threading;179,168using System.ComponentModel;160,681using System.Web;160,323using System.Windows.Forms;137,003using System.Globalization;132,113using System.Drawing;127,033

最后,using 声明不在SystemMicrosoftWindows命名空间的前10名统计结果 :

using 声明总计using NUnit.Framework;119,463using UnityEngine;117,673using Xunit;99,099using Newtonsoft.Json;81,675using Newtonsoft.Json.Linq;29,416using Moq;23,546using UnityEngine.UI;20,355using UnityEditor;19,937using Amazon.Runtime;18,941using log4net;17,297

哪些NuGet软件包常常被包含在.NET项目中

还有一个单独的数据集包含GitHub上的所有‘packages.config’文件,它被称为contents_net_packages_config,有104,808个条目。通过查询,我们可以看到Json.Net是完胜的赢家!

package总计Newtonsoft.Json45,055Microsoft.Web.Infrastructure16,022Microsoft.AspNet.Razor15,109Microsoft.AspNet.WebPages14,495Microsoft.AspNet.Mvc14,236EntityFramework14,191Microsoft.AspNet.WebApi.Client13,480Microsoft.AspNet.WebApi.Core12,210Microsoft.Net.Http11,625jQuery10,646Microsoft.Bcl.Build10,641Microsoft.Bcl10,349NUnit10,341Owin9,681Microsoft.Owin9,202Microsoft.AspNet.WebApi.WebHost9,007WebGrease8,743Microsoft.AspNet.Web.Optimization8,721Microsoft.AspNet.WebApi8,179

一般C#文件中有多少行代码(LOC)?

C#开发人员是否容易创建巨大的文件,可以为1000的行?

注意Y轴是“代码行”,原始数据。

您是否想知道十大最大的C#文件是哪些呢!

文件行数MarMot/Input/test.marmot.cs92663src/CodenameGenerator/WordRepos/LastNamesRepository.cs88810cs_inputtest/cs_02_7000.cs63004cs_inputtest/cs_02_6000.cs54004src/ML NET20/Utility/UserName.cs52014MWBS/Dictionary/DefaultWordDictionary.cs48912Sources/Accord.Math/Matrix/Matrix.Comparisons1.Generated.cs48407UrduProofReader/UrduLibs/Utils.cs48255cs_inputtest/cs_02_5000.cs45004css/style.cs44366

使用最广泛的“Exception”是哪一个?

这里有一些有趣的结果(查询方式),谁知道这么多ApplicationExceptions被抛出来,NotSupportedException这么高令人有点担心!

Exception总计throw new ArgumentNullException699,526throw new ArgumentException361,616throw new NotImplementedException340,361throw new InvalidOperationException260,792throw new ArgumentOutOfRangeException160,640throw new NotSupportedException110,019throw new HttpResponseException74,498throw new ValidationException35,615throw new ObjectDisposedException31,129throw new ApplicationException30,849throw new UnauthorizedException21,133throw new FormatException19,510throw new SerializationException17,884throw new IOException15,779throw new IndexOutOfRangeException14,778throw new NullReferenceException12,372throw new InvalidDataException12,260throw new ApiException11,660throw new InvalidCastException10,510

使用“async/await”?

在C#语言中使用asyncawait关键字编写异步代码更容易:

    public async Task<int> GetDotNetCountAsync()    {        // Suspends GetDotNetCountAsync() to allow the caller (the web server)        // to accept another request, rather than blocking on this one.        var html = await _httpClient.DownloadStringAsync("http://dotnetfoundation.org");            return Regex.Matches(html, ".NET").Count;    }

但它使用了多少?使用下面的查询:

SELECT Count(*) count    FROM      [fh-bigquery:github_extracts.contents_net_cs]    WHERE      REGEXP_MATCH(content, r'\sasync\s|\sawait\s')

我发现有218,643个文件(总共5,885,933个)至少使用一个asyncawait关键字。

C#开发人员是否喜欢使用“var”关键字?

asyncawait关键字使用的更少,只有130,590个文件至少有一个var关键字被使用。


在一个Repository中一般有多少个文件?

90%的Repository(具有任何C#文件)具有95个或更少的文件。95%具有170个文件或更少,99%具有535个或更少的文件。

(Y轴(C##文件)是个数)

根据C#文件数量排名前10的最大Repository如下所示:

RepositoryC#文件数https://github.com/xen2/mcs23389https://github.com/mater06/LEGOChimaOnlineReloaded14241https://github.com/Microsoft/referencesource13051https://github.com/dotnet/corefx10652https://github.com/apo-j/Projects_Working10185https://github.com/Microsoft/CodeContracts9338https://github.com/drazenzadravec/nequeo8060https://github.com/ClearCanvas/ClearCanvas7946https://github.com/mwilliamson-firefly/aws-sdk-net7860https://github.com/151706061/MacroMedicalSystem7765

最受欢迎的Repository是哪一些(其中有C#代码)?

这次我们将要统计包含至少50个C#文件(查询方式)的最受欢迎的Repository(基于GitHub'stars' ):

Repositorystars文件数https://github.com/grpc/grpc11075237https://github.com/dotnet/coreclr85766503https://github.com/dotnet/roslyn84226351https://github.com/facebook/yoga804673https://github.com/bazelbuild/bazel7123132https://github.com/dotnet/corefx711510652https://github.com/SeleniumHQ/selenium7024512https://github.com/Microsoft/WinObjC618481https://github.com/qianlifeng/Wox5674207https://github.com/Wox-launcher/Wox5674142https://github.com/ShareX/ShareX5336766https://github.com/Microsoft/Windows-universal-samples51301501https://github.com/NancyFx/Nancy3701957https://github.com/chocolatey/choco3432248https://github.com/JamesNK/Newtonsoft.Json3340650

有趣的是,第一名是Google Repository!(其中的C#文件是使用.NET中的GRPC库的示例代码)

最受欢迎的C# Class 名称中哪些?

假设我使用正则表达式,最流行的C# class名称如下:

Class 名称总计class C182480class Program163462class Test50593class Settings40841class Resources39345class A34687class App28462class B24246class Startup18238class Foo15198

Yay Foo,偷偷进入前10!

'Foo.cs','Program.cs'或其它,最常见的文件是哪些?

最后,我们来看看class使用的不同的名称,就像using声明作为Visual Studio默认模板一样:

文件总计AssemblyInfo.cs386822Program.cs105280Resources.Designer.cs40881Settings.Designer.cs35392App.xaml.cs21928Global.asax.cs16133Startup.cs14564HomeController.cs13574RouteConfig.cs11278MainWindow.xaml.cs11169

原文:《Analysing C# code on GitHub with BigQuery》http://mattwarren.org/2017/10/12/Analysing-C-code-on-GitHub-with-BigQuery/
翻译:Sweet Tang
本文地址:http://www.cnblogs.com/tdfblog/p/Analysing-C-code-on-GitHub-with-BigQuery.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注