您的位置:首页 > 编程语言

让Visual Studio的代码分析工具提高你的代码质量

2012-11-07 19:07 549 查看
静态代码分析工具,第三方的有很多。其实visual studio有的版本里面也有的哦,通过Q—A的引子希望本文可以起到抛砖引玉的作用。

Q:为什么我的Visual Studio中没有代码分析功能([分析]菜单中[仅对...运行代码分析])。

A:你的vs版本是下面其中之一吗?

   Microsoft Visual Studio 2005 Team Edition for Software Developers

   Visual Studio Team System Development Edition 或 Visual Studio Team Suite

   Visual Studio 2010 Premium 或 Visual Studio 2010 Team Suite

   Visual Studio 2012 Premium 或 Visual Studio 2012 Ultimate 

Q:代码分析得出的都是些什么样的结果?

A:[项目]菜单--》[属性]--》[配置属性]--》[代码分析]--》[托管代码分析规则]里有详细的规则,下面是几个例子。

   warning C6326: Potential comparison of a constant with another constant

   warning C6202: Buffer overrun for '...', which is possibly stack allocated, in call to 'memset': length 'xxx' exceeds buffer size 'yyy'

   warning C6386: Buffer overrun: accessing 'argument 1', the writable size is 'xxxx' bytes, but 'yyy' bytes might be written: Lines: ... ... ...

   warning C6385: Invalid data: accessing 'argument 2', the readable size is 'xxx' bytes, but 'yyy' bytes might be read: Lines: ... ... ... 

Q:分析得出的这些警告后,我们要做什么?

A:一句话,找到根本原因,修改使之更安全;更进一步,努力写出不被代码分析工具警告的代码。

   之所以重要,是因为这些警告很可能会导致Release版本出类似buffer overrun之类难搞的问题,而且这些问题多数在debug下并不再现。

Q:msdn官方链接

A:http://msdn.microsoft.com/zh-cn/library/4dtdybt8(v=vs.90).aspx

   链接的标题即为“编写高质量的代码”,有下面这样一段话。

   {从一开始就确保质量。质量不是您可以在以后容易地增添的元素。过于复杂、过于隐蔽或在产品生命周期中发现过迟的问题通常得不到修复。}

   {Ensure quality from the start. Quality is not something that can be easily added later. Problems that are too complex, too obscure, or are discovered too late in the product cycle are usually not fixed.}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息