如何在 .NET 中的算法代码中进行断言?

发布于 2024-10-03 11:11:23 字数 525 浏览 1 评论 0原文

我目前正在为一个大学项目使用 C# 开发一个小型人工智能框架(遗传算法/神经网络)。

我首先关心的是开发一个可重用的框架,因此我将所有内容设计得非常模块化。我知道我为此(性能)付出了代价,但我觉得我得到的比失去的多(最好是让代码慢两倍,而不是稍后试图发现不可能的时间)发现错误并花费大量时间尝试添加难以在整体代码块中引入的新内容)。

基本上,我想对代码的不同部分、断言进行大量检查。检查运行方法 X 时是否确实处于正确的状态等。这些断言在开发时很有用,但我希望它们远离发布代码(也就是说,当我决定要离开时)为了得到我的最终研究结果而工作了一个晚上)。

我可以看到实现此目的的几种方法:

  1. System.Diagonists.Assert 方法系列。
  2. 代码契约
  3. if (x) then throw InvalidStateException() 被 #if DEBUG / #endif 包围

你会怎么做,为什么?

我也知道单元测试(我正在使用它们),但我也想对代码进行某种断言。

I am currently developing a small AI framework ( genetic algorithms / neural networks) in C#, for a university project.

My first concern is developing a reusable framework, so I am designing everything to be pretty modular. I know I am paying off a price for this (performance), but I feel that I have more to gain than to lose with it (preferable to have code twice as slow than have to lose twice as much time later trying to find impossible to find bugs and losing lots of time trying to add new things that are hard to introduce in a monolithic block of code).

I would like to have lots of checks on different parts of my code, assertions, basically. Check if when running method X I am indeed in a correct state, etc. Those kind of assertions are useful when developing, but I'd like them to stay away from release code (that is, when I decide that I'll want to leave this working for the night to get my final research results).

I can see several ways of accomplishing this:

  1. System.Diagonists.Assert family of methods.
  2. Code Contracts
  3. Having if (x) then throw InvalidStateException() surrounded by #if DEBUG / #endif

How would you do it and why?

I am also aware of Unit-Tests (I am using them), but I'd also like to have some kind of assertions on the code, too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅黛梨妆こ 2024-10-10 11:11:23

您可以使用如下所示的静态方法:

[Conditional("DEBUG")]
public static void Assert(bool condition, string message)
{
    if (!condition)
        throw new InvalidStateException("Assertion failed: " + message);
}

并像这样断言,假设该方法是在名为 Util 的类中定义的:

Util.Assert(a == b, "a == b");

只有在设置了 DEBUG 符号时,编译器才会发出对此方法的调用,感谢条件属性。因此,您不需要将此类调用包装在任何 #if 指令中。这将减少代码混乱。

(请注意,该方法本身仍将被编译。这允许您在不同的程序集中使用此类方法!)

You could use a static method like this:

[Conditional("DEBUG")]
public static void Assert(bool condition, string message)
{
    if (!condition)
        throw new InvalidStateException("Assertion failed: " + message);
}

And assert like so, assuming the method is defined in a class called Util:

Util.Assert(a == b, "a == b");

Calls to this method will only be emitted by the compiler when the DEBUG symbol is set, thanks to the ConditionalAttribute. So you don't need to wrap such calls in any #if directives. This will lead to less code clutter.

(Note that the method itself will still be compiled. This allows you to use such methods in different assemblies!)

江挽川 2024-10-10 11:11:23

#if/#endif 的优点在于,不仅消除了对断言函数的调用,而且还消除了准备其参数的所有工作(希望不需要副作用...)。

The advantage of #if/#endif is that not only is the call to the assert function eliminated, but also all the work preparing its parameters (which hopefully don't have side effects...).

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