面向对象代码分析的思路

发布于 2024-12-03 07:29:34 字数 603 浏览 2 评论 0原文

如果我能变魔术,我会想出一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-12-10 07:29:34

代码合同基本上可以满足您的要求。 (http://msdn.microsoft.com/en-us/devlabs/dd491992)

代码契约允许您使用属性和调用来修饰代码,从而允许编译器和 IDE 静态分析您的代码。您可以在 System.Diagnostics.Contracts 命名空间中找到代码契约,但要利用完整的静态类型检查,您至少需要 Visual Studio 的高级版 SKU(我认为)。

举一个简单的例子,您的 Functional 属性本质上与 Pure 相同:

[Pure]
public void GetMessage() { return _message; }

它告诉分析器该方法不会更改状态。您还可以对您的方法执行前置和后置条件,例如:

public void WriteMessage(string message)
{
    Contract.Requires(message != null);
}

代码契约中有很多深度,值得一读。

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 as Pure:

[Pure]
public void GetMessage() { return _message; }

Which tells the analyser that the method makes no state changes. You can also do pre and post conditions on your methods, e.g.:

public void WriteMessage(string message)
{
    Contract.Requires(message != null);
}

There is a lot of depth in Code Contracts, and worth a good reading.

娇俏 2024-12-10 07:29:34

静态分析工具 NDepend 几乎可以完成您所描述的工作。看一下这个默认的代码查询规则,它查找(因为用PureAttribute标记)和不再的方法:

// <Name>Regression on pure methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
  HasAttribute "OPTIONAL:NDepend.CQL.PureAttribute" AND 
  ( ChangesObjectState OR ChangesTypeState ) AND
  NbLinesOfCode > 0

// A method is pure if its execution doesn’t change 
// the value of any instance or static field. 
// Pure methods naturally simplify code by limiting 
// side-effects.
// See some explanations on immutability - purity and 
// how NDepend supports it here:
// http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx

// NDepend.CQL.PureAttribute is defined in the 
// redistributable assembly $NDependInstallDir$\Lib\NDepend.CQL.dll
// You can define your own attribute to tag 'pure' methods.

请注意,您可以使用自己的 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:

// <Name>Regression on pure methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
  HasAttribute "OPTIONAL:NDepend.CQL.PureAttribute" AND 
  ( ChangesObjectState OR ChangesTypeState ) AND
  NbLinesOfCode > 0

// A method is pure if its execution doesn’t change 
// the value of any instance or static field. 
// Pure methods naturally simplify code by limiting 
// side-effects.
// See some explanations on immutability - purity and 
// how NDepend supports it here:
// http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx

// NDepend.CQL.PureAttribute is defined in the 
// redistributable assembly $NDependInstallDir$\Lib\NDepend.CQL.dll
// You can define your own attribute to tag 'pure' methods.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文