Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio Visual Studio代码度量误报代码行_Visual Studio_Out_Code Metrics_Lines Of Code - Fatal编程技术网

Visual studio Visual Studio代码度量误报代码行

Visual studio Visual Studio代码度量误报代码行,visual-studio,out,code-metrics,lines-of-code,Visual Studio,Out,Code Metrics,Lines Of Code,Visual Studio中的代码度量分析器以及代码度量power工具将以下代码的TestMethod方法中的代码行数报告为8 最多,我希望它将代码行报告为3行 [TestClass] 公共类UnitTest1 { 专用无效测试(输出字符串str) { str=null; } [测试方法] 公共void TestMethod() { var mock=new mock(); 字符串str; 模拟验证(m=>m.Test(out-str)); } } 有人能解释为什么会这样吗 进一步信息 在进一

Visual Studio中的代码度量分析器以及代码度量power工具将以下代码的
TestMethod
方法中的代码行数报告为8

最多,我希望它将代码行报告为3行

[TestClass]
公共类UnitTest1
{
专用无效测试(输出字符串str)
{
str=null;
}
[测试方法]
公共void TestMethod()
{
var mock=new mock();
字符串str;
模拟验证(m=>m.Test(out-str));
}
}
有人能解释为什么会这样吗

进一步信息

在进一步挖掘之后,我发现从测试方法中删除
out
参数并更新测试代码会导致LOC报告为2,我认为这是正确的。添加
out
会导致跳转,因此这不是因为大括号或属性


使用dotPeek反编译DLL会显示由于
out
参数而生成的大量额外代码,该参数可被视为8 LOC,但删除该参数并反编译也会显示生成的代码,该代码可被视为5 LOC,因此这不仅仅是VS计算编译器生成的代码的问题(我认为它无论如何都不应该这样做)。

对“代码行”(LOC)有几种常见的定义。每种定义都试图让我认为几乎毫无意义的指标变得有意义。例如,谷歌的有效代码行(eLOC)

我认为VS将属性作为方法声明的一部分,并试图通过计算语句甚至大括号来给出eLOC。一种可能性是'm=>m.Test(out str)'被计算为语句

考虑这一点:

if (a > 1 &&
    b > 2)
{
   var result;
   result = GetAValue();
   return result;
}
int varA = 0;
varA = GetAValue();
这是:

if (a> 1 && b >2)
   return GetAValue();
var varA = GetAValue();
LOC的一个定义是对包含任何代码的行进行计数。这甚至可能包括大括号。在这样一个极端简单的定义中,计数在编码风格上差异很大

eLOC试图减少或消除代码风格的影响。例如,正如这里的情况一样,一个声明可能被算作一行。不是证明它是正确的,只是解释

考虑这一点:

if (a > 1 &&
    b > 2)
{
   var result;
   result = GetAValue();
   return result;
}
int varA = 0;
varA = GetAValue();
这是:

if (a> 1 && b >2)
   return GetAValue();
var varA = GetAValue();
两行还是一行

这一切都归结为目的是什么。如果是为了测量你需要的显示器有多高,那么也许可以使用一个简单的LOC。如果是为了测量复杂性,那么也许计数代码语句更好,比如eLOC

如果你想测量复杂度,那么就使用一个复杂度度量,比如圈复杂度。不要担心VS是如何测量LOC的,因为我认为它是一个无用的度量。

使用该工具,我们得到了TestMethod()的2行代码(LOC)(免责声明我是该工具的开发人员之一).我写了一篇关于这一点的文章,阐明了什么是逻辑的LoC,以及所有的。净LoC计数工具依赖于PDB序列点技术

关于VS metric提供的LoC值8,我的猜测是,它包括lambda表达式生成的方法的LoC+它包括与开放/结束大括号相关的PDB序列点(NDepend没有)编译器也做了大量的练习来完成所谓的
str
,但这不应影响从PDB序列点推断的#LoC

顺便说一句,我写了另外两篇与LoC相关的文章:


我想知道Visual Studio的行数计算,为什么我看到的不是报告的内容。因此我编写了一个小型C#console程序来计算纯代码行数,并将结果写入CSV文件(见下文)

打开一个新的解决方案,将其复制并粘贴到Program.cs文件中,生成可执行文件,然后就可以开始了。这是一个.Net 3.5应用程序。将其复制到代码库的最顶层目录中。打开命令窗口并运行可执行文件。您会得到两个提示,首先是输入程序/子系统的名称,然后输入您想要的任何额外文件类型分析。然后将结果写入当前目录中的CSV文件。对于您的目的或交给管理层来说,这是一件非常简单的事情

不管怎样,在这里,FWIW和YMMV:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace CodeMetricsConsole
{
    class Program
    {
        // Concept here is that the program has a list of file extensions to do line counts on; it
        // gets any extra extensions at startup from the user. Then it gets a list of files based on
        // each extension in the current directory and all subdirectories. Then it walks through 
        // each file line by line and will display counts for that file and for that file extension.
        // It writes that information to a CSV file in the current directory. It uses regular expressions
        // on each line of each file to figure out what it's looking at, and how to count it (i.e. is it
        // a line of code, a single or multi line comment, a multi-line string, or a whitespace line).
        // 
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(); // spacing

                // prompt user for subsystem or application name
                String userInput_subSystemName;
                Console.Write("Enter the name of this application or subsystem (required): ");
                userInput_subSystemName = Console.ReadLine();

                if (userInput_subSystemName.Length == 0)
                {
                    Console.WriteLine("Application or subsystem name required, exiting.");
                    return;
                }

                Console.WriteLine(); // spacing

                // prompt user for additional types
                String userInput_additionalFileTypes;
                Console.WriteLine("Default extensions are asax, css, cs, js, aspx, ascx, master, txt, jsp, java, php, bas");
                Console.WriteLine("Enter a comma-separated list of additional file extensions (if any) you wish to analyze");
                Console.Write(" --> ");
                userInput_additionalFileTypes = Console.ReadLine();

                // tell user processing is starting
                Console.WriteLine();
                Console.WriteLine("Getting LOC counts...");
                Console.WriteLine();

                // the default file types to analyze - hashset to avoid duplicates if the user supplies extensions
                HashSet allowedExtensions = new HashSet { "asax", "css", "cs", "js", "aspx", "ascx", "master", "txt", "jsp", "java", "php", "bas" };

                // Add user-supplied types to allowedExtensions if any
                String[] additionalFileTypes;
                String[] separator = { "," };
                if (userInput_additionalFileTypes.Length > 0)
                {
                    // split string into array of additional file types
                    additionalFileTypes = userInput_additionalFileTypes.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                    // walk through user-provided file types and append to default file types
                    foreach (String ext in additionalFileTypes)
                    {
                        try
                        {
                            allowedExtensions.Add(ext.Trim()); // remove spaces
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception: " + e.Message);
                        }
                    }
                }

                // summary file to write to
                String summaryFile = userInput_subSystemName + "_Summary.csv";
                String path = Directory.GetCurrentDirectory();
                String pathAndFile = path + Path.DirectorySeparatorChar + summaryFile;

                // regexes for the different line possibilities
                Regex oneLineComment = new Regex(@"^\s*//"); // match whitespace to two slashes
                Regex startBlockComment = new Regex(@"^\s*/\*.*"); // match whitespace to /*
                Regex whiteSpaceOnly = new Regex(@"^\s*$"); // match whitespace only
                Regex code = new Regex(@"\S*"); // match anything but whitespace
                Regex endBlockComment = new Regex(@".*\*/"); // match anything and */ - only used after block comment detected
                Regex oneLineBlockComment = new Regex(@"^\s*/\*.*\*/.*"); // match whitespace to /* ... */
                Regex multiLineStringStart = new Regex("^[^\"]*@\".*"); // match @" - don't match "@"
                Regex multiLineStringEnd = new Regex("^.*\".*"); // match double quotes - only used after multi line string start detected
                Regex oneLineMLString = new Regex("^.*@\".*\""); // match @"..."
                Regex vbaComment = new Regex(@"^\s*'"); // match whitespace to single quote

                // Uncomment these two lines to test your regex with the function testRegex() below
                //new Program().testRegex(oneLineMLString);
                //return;

                FileStream fs = null;
                String line = null;
                int codeLineCount = 0;
                int commentLineCount = 0;
                int wsLineCount = 0;
                int multiLineStringCount = 0;
                int fileCodeLineCount = 0;
                int fileCommentLineCount = 0;
                int fileWsLineCount = 0;
                int fileMultiLineStringCount = 0;
                Boolean inBlockComment = false;
                Boolean inMultiLineString = false;

                try
                {
                    // write to summary CSV file, overwrite if exists, don't append
                    using (StreamWriter outFile = new StreamWriter(pathAndFile, false))
                    {
                        // outFile header
                        outFile.WriteLine("filename, codeLineCount, commentLineCount, wsLineCount, mlsLineCount");

                        // walk through files with specified extensions
                        foreach (String allowed_extension in allowedExtensions)
                        {
                            String extension = "*." + allowed_extension;

                            // reset accumulating values for extension
                            codeLineCount = 0;
                            commentLineCount = 0;
                            wsLineCount = 0;
                            multiLineStringCount = 0;

                            // Get all files in current directory and subdirectories with specified extension
                            String[] fileList = Directory.GetFiles(Directory.GetCurrentDirectory(), extension, SearchOption.AllDirectories);

                            // walk through all files of this type
                            for (int i = 0; i < fileList.Length; i++)
                            {
                                // reset values for this file
                                fileCodeLineCount = 0;
                                fileCommentLineCount = 0;
                                fileWsLineCount = 0;
                                fileMultiLineStringCount = 0;
                                inBlockComment = false;
                                inMultiLineString = false;

                                try
                                {
                                    // open file
                                    fs = new FileStream(fileList[i], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                                    using (TextReader tr = new StreamReader(fs))
                                    {
                                        // walk through lines in file
                                        while ((line = tr.ReadLine()) != null)
                                        {
                                            if (inBlockComment)
                                            {
                                                if (whiteSpaceOnly.IsMatch(line))
                                                {
                                                    fileWsLineCount++;
                                                }
                                                else
                                                {
                                                    fileCommentLineCount++;
                                                }

                                                if (endBlockComment.IsMatch(line)) inBlockComment = false;
                                            }
                                            else if (inMultiLineString)
                                            {
                                                fileMultiLineStringCount++;

                                                if (multiLineStringEnd.IsMatch(line)) inMultiLineString = false;
                                            }
                                            else
                                            {
                                                // not in a block comment or multi-line string
                                                if (oneLineComment.IsMatch(line))
                                                {
                                                    fileCommentLineCount++;
                                                }
                                                else if (oneLineBlockComment.IsMatch(line))
                                                {
                                                    fileCommentLineCount++;
                                                }
                                                else if ((startBlockComment.IsMatch(line)) && (!(oneLineBlockComment.IsMatch(line))))
                                                {
                                                    fileCommentLineCount++;
                                                    inBlockComment = true;
                                                }
                                                else if (whiteSpaceOnly.IsMatch(line))
                                                {
                                                    fileWsLineCount++;
                                                }
                                                else if (oneLineMLString.IsMatch(line))
                                                {
                                                    fileCodeLineCount++;
                                                }
                                                else if ((multiLineStringStart.IsMatch(line)) && (!(oneLineMLString.IsMatch(line))))
                                                {
                                                    fileCodeLineCount++;
                                                    inMultiLineString = true;
                                                }
                                                else if ((vbaComment.IsMatch(line)) && (allowed_extension.Equals("txt") || allowed_extension.Equals("bas"))
                                                {
                                                    fileCommentLineCount++;
                                                }
                                                else
                                                {
                                                    // none of the above, thus it is a code line
                                                    fileCodeLineCount++;
                                                }
                                            }
                                        } // while

                                        outFile.WriteLine(fileList[i] + ", " + fileCodeLineCount + ", " + fileCommentLineCount + ", " + fileWsLineCount + ", " + fileMultiLineStringCount);

                                        fs.Close();
                                        fs = null;

                                    } // using
                                }
                                finally
                                {
                                    if (fs != null) fs.Dispose();
                                }

                                // update accumulating values
                                codeLineCount = codeLineCount + fileCodeLineCount;
                                commentLineCount = commentLineCount + fileCommentLineCount;
                                wsLineCount = wsLineCount + fileWsLineCount;
                                multiLineStringCount = multiLineStringCount + fileMultiLineStringCount;

                            } // for (specific file)

                            outFile.WriteLine("Summary for: " + extension + ", " + codeLineCount + ", " + commentLineCount + ", " + wsLineCount + ", " + multiLineStringCount);

                        } // foreach (all files with specified extension)

                    } // using summary file streamwriter

                    Console.WriteLine("Analysis complete, file is: " + pathAndFile);

                } // try block
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
            catch (Exception e2)
            {
                Console.WriteLine("Error: " + e2.Message);
            }

        } // main


        // local testing function for debugging purposes
        private void testRegex(Regex rx)
        {
            String test = "        asdfasd asdf @\"     adf ++--// /*\" ";

            if (rx.IsMatch(test))
            {
                Console.WriteLine(" -->| " + rx.ToString() + " | matched: " + test);
            }
            else
            {
                Console.WriteLine("No match");
            }
        }

    } // class
} // namespace


使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.IO;
命名空间CodeMetricsConsole
{
班级计划
{
//这里的概念是程序有一个文件扩展名列表来进行行计数;它
//在启动时从用户处获取任何额外的扩展名。然后它将根据
//当前目录和所有子目录中的每个扩展
//每个文件逐行显示,并将显示该文件和该文件扩展名的计数。
//它将该信息写入当前目录中的CSV文件。它使用正则表达式
//在每个文件的每一行上,找出它在看什么,以及如何计算它(即它是
//一行代码、一行或多行注释、多行字符串或空白行)。
// 
静态void Main(字符串[]参数)
{
尝试
{
Console.WriteLine();//间距
//提示用户输入子系统或应用程序名称
字符串userInput_subSystemName;
控制台。写入(“输入此应用程序或子系统的名称(必需):”;
userInput_subSystemName=Console.ReadLine();
if(userInput_subSystemName.Length==0)
{
WriteLine(“需要应用程序或子系统名称,正在退出”);
返回;
}
Console.WriteLine();//间距
//提示用户输入其他类型
字符串userInput\u附加文件类型;
控制台.Wr