欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > C# >内容正文

C#

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

发布时间:2023/12/4 C# 76 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用BigQuery分析GitHub上的C#代码 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一年多以前,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#源代码!


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

  • Tab或空格?

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

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

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

  • 哪些using 声明使用最广泛?

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

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

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

  • 使用“async/await”?

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

  • 在我们最终看到Repository:

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

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

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

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

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


    Tab或空格?

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

    Tabs Tab(百分比) 空格 空格(百分比) 总计
    799,055 17.15% 3,859,528 82.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,646
    using System; 1,477,019
    using System.Linq; 1,319,830
    using System.Text; 902,165
    using System.Threading.Tasks; 628,195
    using System.Runtime.InteropServices; 431,867
    using System.IO; 407,848
    using System.Runtime.CompilerServices; 338,686
    using System.Collections; 289,867
    using System.Reflection; 218,369

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

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

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

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

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

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

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

    package 总计
    Newtonsoft.Json 45,055
    Microsoft.Web.Infrastructure 16,022
    Microsoft.AspNet.Razor 15,109
    Microsoft.AspNet.WebPages 14,495
    Microsoft.AspNet.Mvc 14,236
    EntityFramework 14,191
    Microsoft.AspNet.WebApi.Client 13,480
    Microsoft.AspNet.WebApi.Core 12,210
    Microsoft.Net.Http 11,625
    jQuery 10,646
    Microsoft.Bcl.Build 10,641
    Microsoft.Bcl 10,349
    NUnit 10,341
    Owin 9,681
    Microsoft.Owin 9,202
    Microsoft.AspNet.WebApi.WebHost 9,007
    WebGrease 8,743
    Microsoft.AspNet.Web.Optimization 8,721
    Microsoft.AspNet.WebApi 8,179

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

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

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

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

    文件 行数
    MarMot/Input/test.marmot.cs 92663
    src/CodenameGenerator/WordRepos/LastNamesRepository.cs 88810
    cs_inputtest/cs_02_7000.cs 63004
    cs_inputtest/cs_02_6000.cs 54004
    src/ML NET20/Utility/UserName.cs 52014
    MWBS/Dictionary/DefaultWordDictionary.cs 48912
    Sources/Accord.Math/Matrix/Matrix.Comparisons1.Generated.cs 48407
    UrduProofReader/UrduLibs/Utils.cs 48255
    cs_inputtest/cs_02_5000.cs 45004
    css/style.cs 44366

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

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

    Exception 总计
    throw new ArgumentNullException 699,526
    throw new ArgumentException 361,616
    throw new NotImplementedException 340,361
    throw new InvalidOperationException 260,792
    throw new ArgumentOutOfRangeException 160,640
    throw new NotSupportedException 110,019
    throw new HttpResponseException 74,498
    throw new ValidationException 35,615
    throw new ObjectDisposedException 31,129
    throw new ApplicationException 30,849
    throw new UnauthorizedException 21,133
    throw new FormatException 19,510
    throw new SerializationException 17,884
    throw new IOException 15,779
    throw new IndexOutOfRangeException 14,778
    throw new NullReferenceException 12,372
    throw new InvalidDataException 12,260
    throw new ApiException 11,660
    throw new InvalidCastException 10,510

    使用“async/await”?

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

       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(*) countFROM[fh-bigquery:github_extracts.contents_net_cs]    WHEREREGEXP_MATCH(content, r'\sasync\s|\sawait\s')

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

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

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


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

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

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

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

    Repository C#文件数
    https://github.com/xen2/mcs 23389
    https://github.com/mater06/LEGOChimaOnlineReloaded 14241
    https://github.com/Microsoft/referencesource 13051
    https://github.com/dotnet/corefx 10652
    https://github.com/apo-j/Projects_Working 10185
    https://github.com/Microsoft/CodeContracts 9338
    https://github.com/drazenzadravec/nequeo 8060
    https://github.com/ClearCanvas/ClearCanvas 7946
    https://github.com/mwilliamson-firefly/aws-sdk-net 7860
    https://github.com/151706061/MacroMedicalSystem 7765

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

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

    Repository stars 文件数
    https://github.com/grpc/grpc 11075 237
    https://github.com/dotnet/coreclr 8576 6503
    https://github.com/dotnet/roslyn 8422 6351
    https://github.com/facebook/yoga 8046 73
    https://github.com/bazelbuild/bazel 7123 132
    https://github.com/dotnet/corefx 7115 10652
    https://github.com/SeleniumHQ/selenium 7024 512
    https://github.com/Microsoft/WinObjC 6184 81
    https://github.com/qianlifeng/Wox 5674 207
    https://github.com/Wox-launcher/Wox 5674 142
    https://github.com/ShareX/ShareX 5336 766
    https://github.com/Microsoft/Windows-universal-samples 5130 1501
    https://github.com/NancyFx/Nancy 3701 957
    https://github.com/chocolatey/choco 3432 248
    https://github.com/JamesNK/Newtonsoft.Json 3340 650

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

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

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

    Class 名称 总计
    class C 182480
    class Program 163462
    class Test 50593
    class Settings 40841
    class Resources 39345
    class A 34687
    class App 28462
    class B 24246
    class Startup 18238
    class Foo 15198

    Yay Foo,偷偷进入前10!

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

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

    文件 总计
    AssemblyInfo.cs 386822
    Program.cs 105280
    Resources.Designer.cs 40881
    Settings.Designer.cs 35392
    App.xaml.cs 21928
    Global.asax.cs 16133
    Startup.cs 14564
    HomeController.cs 13574
    RouteConfig.cs 11278
    MainWindow.xaml.cs 11169

    原文:《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跨平台或扫描二维码关注

    总结

    以上是生活随笔为你收集整理的使用BigQuery分析GitHub上的C#代码的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。