Debug.Assert的 [英] Debug.Assert

查看:61
本文介绍了Debug.Assert的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中,我曾经使用Assert作为开发辅助工具。在调试期间,它将确保满足某些条件,但在Release版本中,Asserts

未编译到应用程序中。我假设这与

..NET也一样吗? Debug.Assert没有编译成Release版本吗?


我很好奇的另一件事就是这个


public void DoSomething(人p)

{

if(p == null)

抛出新的ArgumentNullException(" P");

....

}


为什么人们倾向于使用诸如ArgumentNullException之类的异常

之类的东西这个我认为在Release版本中被跳过并且

也可以更快地输入?


public void DoSomething(Person p)

{

Debug.Assert(p!= null,P is null);

....

}


Pete

In Delphi I used to use Assert as a development aid. During debug it would
ensure that certain conditions were met but in the Release build the Asserts
were not compiled into the application. I am assuming this is the same with
..NET too? Debug.Assert isn''t compiled into a Release build?

The other thing I am curious about is this

public void DoSomething(Person p)
{
if (p == null)
throw new ArgumentNullException("P");
....
}

Why do people tend to use exceptions such as ArgumentNullException instead
of something like this which I presume gets skipped in Release build and is
also quicker to type?

public void DoSomething(Person p)
{
Debug.Assert(p != null, "P is null");
....
}

Pete

推荐答案

Debug.Assert标有[Conditional(DEBUG)],这意味着那个

调用只有在调用者定义了DEBUG符号时才会被编译。


所以是的;对Debug.Assert的调用不会编译成发布版本,只要你没有定义DEBUG,就可以编译
。 ; -p


重新检查;好吧,我想这取决于代码是什么。如果它是
是一个库,你不能信任调用者,所以你还需要检查。

对于UI工作,你可以使用Assert ,但你需要确保

你有100%的代码覆盖率。但是这张支票很快就可以了,所以我可能会把它留在大多数情况下。


重新输入,好吧,你可以使用一个自定义片段,如果你想要...在C#3中,你也可以使用扩展方法(令人困惑

其实:你可以在空实例上调用扩展方法) - 所以你可能有:


person.ThrowIfNull(person);


(注意我将变量重命名为有意义;我' '推荐这个......

从长远来看,它会减轻痛苦。)


Marc
Debug.Assert is marked with [Conditional("DEBUG")], which means that
calls to it only get compiled if the caller has the DEBUG symbol defined.

So yes; calls to Debug.Assert will not be compiled into a release build,
as long as you don''t define "DEBUG" ;-p

Re the null check; Well, I guess it depends on what the code is. If it
is a library you can''t trust the caller, so you''d need to still check.
For UI work, you could argualy use an Assert, but you''d need to be sure
that you have 100% code coverage. But this check will be ridiculously
quick, so I''d probably leave it in in most cases.

Re typing, well, you could probably use a custom snippet if you
wanted... in C# 3 you could also use an extension method (confusing
fact: you can call extension methods on null instances) - so you might have:

person.ThrowIfNull("person");

(note I renamed the variable to be meaningful; I''d recommend this...
it''ll save pain in the long run).

Marc


为什么人们倾向于使用诸如ArgumentNullException之类的异常而不是
Why do people tend to use exceptions such as ArgumentNullException instead

这样的东西我认为在Release版本中被跳过并且

也可以更快地输入?


public void DoSomething(Person p)

{

Debug.Assert(p != null,P为null);

....

}
of something like this which I presume gets skipped in Release build and
is also quicker to type?

public void DoSomething(Person p)
{
Debug.Assert(p != null, "P is null");
....
}



这真的归结为它是否会发生在你的

发布版本中。如果您正在向第三方提供发布库

,那么您永远不会知道某人可能会通过什么。 OTOH,如果它只是被你自己的代码所消耗,那么就是Assert()。通常很好

IMO。有些人仍然认为应该使用例外,但是如果只是为了捕捉你自己的错误(那些使它成为发布版本由

意外发生的错误)。然而,在我长期的经历中,我不记得曾经遇到过这种情况。因此,我依赖于Assert()。广泛地用于我自己的所有

代码。也许它不是那么安全,但是依赖异常来解决发布版本中的程序员错误只会让我误解错误。 GIGO应该在开始编程时需要勤奋的开发人员理解。

Assert()对于这个IMO来说通常就足够了(即使对于第三方

开发者来说也是如此)但它确实是个人选择。

It really boils down to whether it''s something that can happen in your
release build or not. If you''re serving up a release library to a 3rd party
in particular then you never know what someone might pass. OTOH, if it''s
only being consumed by your own code, then an "Assert()" is usually fine
IMO. Some would still argue that an exception should be used however if only
to catch your own mistakes (those that make it into the release build by
accident). In my long experience however, I don''t recall ever encountering
this situation. I therefore rely on "Assert()" extensively for all my own
code. Maybe it''s not quite as safe, but relying on exceptions to catch
programmer errors in a release build just rubs me the wrong way. GIGO should
be understood by developers who need to apply diligence when they program.
An "Assert()" is normally sufficient for this IMO (even for 3rd party
developers for that matter) but it''s really a personal choice.


那些是我过去强加给自己的规则,所以我很高兴看到一些回复确认我做的是正确的事情:-)
Those were the rules I have imposed on myself in the past, so I am pleased
to see a couple of replies confirming that I am doing the right thing :-)

(注意我将变量重命名为有意义;我会建议这样做......从长远来看,它会节省很多痛苦)。
(note I renamed the variable to be meaningful; I''d recommend this... it''ll
save pain in the long run).



我在新闻组写作时更加空闲;-)

谢谢


Pete

I am more idle when writing in newsgroups ;-)
Thanks

Pete


这篇关于Debug.Assert的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆