如何计算 C# 项目的代码注释比?

How do I calculate the code-to-comment ratio of a C# project?

注意:我不是问what the golden code-to-comment ratio is, nor am I attempting to affix a particular ratio to our team. Instead, we would like to improve how well our codebase is documented (we started with a "code should document itself" mentality), which can be accomplished either by removing dead code or by adding comments to live code, and we would like to measure how well we are going about doing that by measuring this ratio multiple times over the course of several months. Also note that I would like to actually measure the amount of comments we have, so something that gets LOC from the generated IL行不通。

我将如何获得 C# 项目的代码与注释比率?我需要编写自己的解析脚本,还是我可以利用 Roslyn 中的某些内容?是否有任何主要的 IDE 直接带有此功能?作为奖励,我可以过滤掉 "punctuation",例如多余的空格、注释分隔符(///* */)和 opening/closing 大括号吗?

您可以使用此正则表达式识别代码中的注释行:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

尝试将其插入 Visual Studio 中的 "Find in Files" 功能以查看其运行情况。

https://regex101.com/r/GCrfzc/1

使用 ,我设法创建了一个简短的 C# 方法,用于根据输入字符串计算此指标。它一个字符一个字符地进行,以便正确说明同时包含代码和注释的行,并且还从度量中排除额外的空格,这样行缩进之类的东西就不算数了。

为了防止灾难性的回溯,我简化了正则表达式(我发现你不需要换行检查,因为字符排除组已经处理了这些)并且还使块注释的主体成为非回溯组.

public static double CodeToCommentRatio(
    string text, 
    out int codeChars, 
    out int commentChars, 
    out int blankChars)
{
    // First, filter out excess whitespace, reporting the number of characters removed this way
    Regex lineStartRegex = new Regex(@"(^|\n)[ \t]+");
    Regex blanksRegex = new Regex(@"[ \t]+");
    string minWhitespaceText = blanksRegex.Replace(lineStartRegex.Replace(text, "\n"), " ");
    blankChars = text.Length - minWhitespaceText.Length;

    // Then, match all comments and report the number of characters in comments
    Regex commentsRegex = new Regex(@"(/\*(?>[^*]|(\*+[^*/]))*\*+/)|(//.*)");
    MatchCollection comments = commentsRegex.Matches(minWhitespaceText);
    commentChars = 0;
    foreach (Match comment in comments)
    {
        commentChars += comment.Length;
    }
    codeChars = minWhitespaceText.Length - commentChars;

    // Finally, return the ratio
    return (double)codeChars / commentChars;
}