面向对象代码分析的思路
如果我能变魔术,我会想出一个 C# 代码分析工具;我们称之为XYZ
。下面是一些代码的示例,您可以将其作为 XYZ
的输入:
public class MyClass
{
private int myInt;
[Functional]
public int GetDoubleOfMyInt()
{
return 2*myInt;
}
[SideEffect: myInt]
public void IncrementMyInt()
{
myInt++;
}
}
请注意这两个方法上的标记。 XYZ
将验证 GetDoubleOfMyInt()
确实是纯函数(从某种意义上说,它仅计算整数),并且 IncrementMyInt
具有副作用为 myInt
赋值。如果交换两个标签 XYZ
将发出两个错误。
我的问题: 1. 类似XYZ
的东西真的存在吗? 2. 如果让你实施,你会从哪里开始?
If I could do magic I would conjure up a C# code analysis tool; let's call it XYZ
. Here's an example of some code you could give as input to XYZ
:
public class MyClass
{
private int myInt;
[Functional]
public int GetDoubleOfMyInt()
{
return 2*myInt;
}
[SideEffect: myInt]
public void IncrementMyInt()
{
myInt++;
}
}
Notice the tags on the two methods. XYZ
would verify that GetDoubleOfMyInt()
is indeed purely functional (in the sense that it merely computes an integer) and that IncrementMyInt
has the side effect of assigning a value to myInt
. If you swapped the two tags XYZ
would issue two errors.
My questions:
1. Does something ressembling XYZ
actually exist?
2. If you were asked to implement it, where would you start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码合同基本上可以满足您的要求。 (http://msdn.microsoft.com/en-us/devlabs/dd491992)
代码契约允许您使用属性和调用来修饰代码,从而允许编译器和 IDE 静态分析您的代码。您可以在 System.Diagnostics.Contracts 命名空间中找到代码契约,但要利用完整的静态类型检查,您至少需要 Visual Studio 的高级版 SKU(我认为)。
举一个简单的例子,您的
Functional
属性本质上与Pure
相同:它告诉分析器该方法不会更改状态。您还可以对您的方法执行前置和后置条件,例如:
代码契约中有很多深度,值得一读。
Code Contracts essentially does what you are asking. (http://msdn.microsoft.com/en-us/devlabs/dd491992)
Code Contracts allow you to decorate your code with attributes and calls that allow the compiler and IDE to statically analyse your code. You can find Code Contracts within the
System.Diagnostics.Contracts
namespace, but to take advantage of full static type checking, you need at least the Premium edition SKU of Visual Studio (I think).A quick example, your
Functional
attribute essentially is the same asPure
:Which tells the analyser that the method makes no state changes. You can also do pre and post conditions on your methods, e.g.:
There is a lot of depth in Code Contracts, and worth a good reading.
静态分析工具 NDepend 几乎可以完成您所描述的工作。看一下这个默认的代码查询规则,它查找纯(因为用PureAttribute标记)和不再纯的方法:
请注意,您可以使用自己的 PureAttribute 而不是规则中默认指定的属性,只需指定您的属性 namespace.nameAttribute 即可。
请注意,CQL(代码查询语言) 子句 ChangesObjectState 和对于分配实例(对象) 或静态(类型) 字段的方法,ChangesTypeState 返回 true。
The static analysis tool NDepend does pretty much what you are describing. Have a look at this default Code Query Rule that find methods that where pure (because tagged with a PureAttribute) and that are not pure anymore:
Notice that you can use your own PureAttribute instead of the one specified by default in the rule, just specify your attribute namespace.nameAttribute.
Notice that the CQL (Code Query Language) clauses ChangesObjectState and ChangesTypeState returns true for methods that assign an instance (object) or a static (type) field.