使用BigQuery分析GitHub上的C#代码
一年多以前,Google 在GitHub中提供了BigQuery用于查詢的GitHub上的開源代碼(open source code on GitHub available for querying),如果這還不夠,您可以免費(fèi)每月運(yùn)行1TB的查詢!
所以在這篇文章中,我將要收集GitHub上的所有我們可以訪問的C#源代碼。非常簡單,只包含C#,而且數(shù)據(jù)集已經(jīng)收集完成(在BigQuery中,是按字節(jié)讀取收費(fèi)的),稱為fh-bigquery:github_extracts.contents_net_cs?并具有
-
5,885,933?不重復(fù)的“.cs”文件
-
792,166,632?行代碼 (LOC)
-
37.17 GB?(37,174,783,891 bytes) 數(shù)據(jù)
這是一套非常全面的C#源代碼!
這篇文章的剩余部分將?嘗試?回答以下問題:
Tab或空格?
regions:“應(yīng)該被禁止”或“在某些情況下使用”?
“K&R” or “Allman”, C#開發(fā)者喜歡把它們的大括號放在哪里?(譯者注:關(guān)于‘K&R’ 與 ‘Allman’,請參考維基百科)
然后轉(zhuǎn)到一些沒有爭議的C#主題:
哪些using 聲明使用最廣泛?
哪些NuGet軟件包常常被包含在.NET項(xiàng)目中
C#文件一般有多少行代碼(LOC)?
使用最廣泛的“Exception”是哪一個(gè)?
使用“async/await”?
C#開發(fā)人員是否喜歡使用“var”關(guān)鍵字?
在我們最終看到Repository:
包含C#代碼最流行的Repository是哪一些
在一個(gè)Repository中一般有多少個(gè)文件?
最受歡迎的C# Class 名稱中哪些?
“Foo.cs”,“Program.cs”或其它,最常見的文件是哪些?
如果您想為自己嘗試查詢(或發(fā)現(xiàn)我的錯(cuò)誤),那么所有這些查詢可以在github上面找到,鏈接地址。很有可能我的正則表達(dá)式遺漏了一些邊緣案例,畢竟正則表達(dá)式:現(xiàn)在有兩個(gè)問題:
Tab或空格?
在整個(gè)數(shù)據(jù)集中有5,885,933個(gè)文件,但是我們只包括一個(gè)從Tab或空格開始的10行以上的文件
| 799,055 | 17.15% | 3,859,528 | 82.85% | 4,658,583 |
顯然,C#開發(fā)人員(在GitHub上)喜歡空格超過Tab?!(我認(rèn)為一些這可以通過Visual Studio中的事實(shí)來解釋:默認(rèn)情況下使用“空格”)。
如果您想了解C#與其它編程語言的比較,請查看40萬個(gè)GitHub資源庫,10億個(gè)文件,14太字節(jié)的代碼:空格或制表符?。
regions:“應(yīng)該被禁止”或“在某些情況下使用”?
事實(shí)證明,有一個(gè)令人印象深刻的712,498 C#文件(總共580萬),至少包含一個(gè)#region語句(查詢方法),只有超過12%。(我希望很多這些文件已被工具自動生成!)
“K&R” or “Allman” ,C#開發(fā)者喜歡把它們的大括號放在哪里?
C#開發(fā)人員絕大多數(shù)喜歡將打開大括號{放在當(dāng)單獨(dú)的行上(查詢方法)
| 81,306,320 (67%) | 40,044,603 (33%) | 3,631,947 (2.99%) | 121,350,923 (15.32%) | 792,166,632 |
(“同一行初始化實(shí)例”包括代碼new { Name = "", .. },new [] { 1, 2, 3.. })
哪些 using 聲明 使用最廣泛?
這是一個(gè)更實(shí)質(zhì)的統(tǒng)計(jì),C#代碼中,using?聲明 最廣泛的語句是哪一些?
顯示前10名結(jié)果(全部統(tǒng)計(jì)結(jié)果在這里查看):
| 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 |
但是,當(dāng)您在Visual Studio中添加新文件時(shí),默認(rèn)情況下包含前5個(gè),許多人不會刪除它們。默認(rèn)情況下,“AssemblyInfo.cs”中包含的“System.Runtime.InteropServices”和“System.Runtime.CompilerServices”也是一樣。
所以如果我們考慮到這一點(diǎn),調(diào)整一下統(tǒng)計(jì)規(guī)則,前10名的結(jié)果是:
| 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名統(tǒng)計(jì)結(jié)果 :
| 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項(xiàng)目中
還有一個(gè)單獨(dú)的數(shù)據(jù)集包含GitHub上的所有‘packages.config’文件,它被稱為contents_net_packages_config,有104,808個(gè)條目。通過查詢,我們可以看到Json.Net是完勝的贏家!
| 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#開發(fā)人員是否容易創(chuàng)建巨大的文件,可以為1000的行?
注意Y軸是“代碼行”,原始數(shù)據(jù)。
您是否想知道十大最大的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”是哪一個(gè)?
這里有一些有趣的結(jié)果(查詢方式),誰知道這么多ApplicationExceptions被拋出來,NotSupportedException這么高令人有點(diǎn)擔(dān)心!
| 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關(guān)鍵字編寫異步代碼更容易:
? ?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')我發(fā)現(xiàn)有218,643個(gè)文件(總共5,885,933個(gè))至少使用一個(gè)async、await關(guān)鍵字。
C#開發(fā)人員是否喜歡使用“var”關(guān)鍵字?
比async、await關(guān)鍵字使用的更少,只有130,590個(gè)文件至少有一個(gè)var關(guān)鍵字被使用。
在一個(gè)Repository中一般有多少個(gè)文件?
90%的Repository(具有任何C#文件)具有95個(gè)或更少的文件。95%具有170個(gè)文件或更少,99%具有535個(gè)或更少的文件。
(Y軸(C##文件)是個(gè)數(shù))
根據(jù)C#文件數(shù)量排名前10的最大Repository如下所示:
| 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#代碼)?
這次我們將要統(tǒng)計(jì)包含至少50個(gè)C#文件(查詢方式)的最受歡迎的Repository(基于GitHub'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 名稱中哪些?
假設(shè)我使用正則表達(dá)式,最流行的C#?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,偷偷進(jìn)入前10!
'Foo.cs','Program.cs'或其它,最常見的文件是哪些?
最后,我們來看看class使用的不同的名稱,就像using聲明作為Visual Studio默認(rèn)模板一樣:
| 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社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的使用BigQuery分析GitHub上的C#代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Azure Cosmos DB技术性解读
- 下一篇: AspectCore中的IoC容器和依赖