一起使用断言和防守 [英] use assert and defensive together

查看:59
本文介绍了一起使用断言和防守的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

请看以下两个代码抢夺:

1.

int foo(const char * some)

{

断言(部分);

if(!some)

{

return - 1; // - 1表示错误

}

...

}


2.

int foo(const char * some)

{

if(!some)

{

断言(一些);

返回-1; // -1表示错误

}

...

}


什么?s你或你的偏好?为什么?我更喜欢第二个

但我无法说服我的朋友。


问候,

hello,
please see following two code snatches:
1.
int foo (const char* some)
{
assert(some);
if (!some)
{
return -1; //-1 indicates error
}
...
}

2.
int foo (const char* some)
{
if (!some)
{
assert(some);
return -1; // -1 indicates error
}
...
}

what?s the preference of you or none of them? why? I prefer the second
but I cannot persuade my friends.

regards,

推荐答案

9月22日,9:25 * pm,bingfeng< bf **** @ gmail.comwrote:
On Sep 22, 9:25*pm, bingfeng <bf****@gmail.comwrote:

hello,

请看以下两个代码抢购:

1.

int foo(const char * some)

{

* *断言(部分);

* * if(!some)

* * {

* * * * return - 1; // - 1表示错误

* *}

* * ...


}

2.

int foo(const char * some)

{

* * if(!some)

* * {

* * * *断言(部分);

* * * *返回-1; // -1表示错误

* *}

* * ...


}

什么是你的偏好还是没有?为什么?我更喜欢第二个

但是我无法说服我的朋友。
hello,
please see following two code snatches:
1.
int foo (const char* some)
{
* * assert(some);
* * if (!some)
* * {
* * * * return -1; //-1 indicates error
* * }
* * ...

}

2.
int foo (const char* some)
{
* * if (!some)
* * {
* * * * assert(some);
* * * * return -1; // -1 indicates error
* * }
* * ...

}

what?s the preference of you or none of them? why? I prefer the second
but I cannot persuade my friends.



他们都会做同样的事情而不管是否某些

评估为真或假,所以这只是风格问题。我会说我好像b $ b更喜欢第一个;第二个似乎是滥用了assert()

宏,因为它总是会终止程序(你已经知道了

会!)。


Sebastian

They both will do the same thing regardless of whether ''some''
evaluates to true or false, so it''s only a matter of style. I''d say I
prefer the first; the second seems like an abuse of the assert()
macro, since it will always terminate the program (you already know it
will!).

Sebastian


2008年9月22日星期一19:25:31 -0700(PDT),bingfeng< bf ** **@gmail.com>

写道:
On Mon, 22 Sep 2008 19:25:31 -0700 (PDT), bingfeng <bf****@gmail.com>
wrote:

> hello,
请看以下两个代码抢夺:
1。
int foo(const char * some)
$

断言(某些);

if(!some)
>hello,
please see following two code snatches:
1.
int foo (const char* some)
{
assert(some);
if (!some)



这个表达式可以评估为真吗?

Can this expression ever evaluate to true?


{

返回-1; // - 1表示错误

}

...
}

2。
int foo(const char *一些)
{

if(!some)

{

断言(某些);

返回-1; // -1表示错误
{
return -1; //-1 indicates error
}
...
}

2.
int foo (const char* some)
{
if (!some)
{
assert(some);
return -1; // -1 indicates error



是否可以达到此声明?

Can this statement ever be reached?


}

...
}

什么是你的偏好还是没有?为什么?我更喜欢第二个
但我无法说服我的朋友。

问候,
}
...
}

what?s the preference of you or none of them? why? I prefer the second
but I cannot persuade my friends.

regards,



-

删除电子邮件的del

--
Remove del for email


bingfeng< bfz ... @ gmail.comwrote:
bingfeng <bfz...@gmail.comwrote:

hello,

请看以下两个代码抢购:

1.

int foo(const char * some)

{

* *断言(部分);

* * if(!some)

* * {

* * * *返回-1; // - 1表示错误

* *}

* * ...

}


2.

int foo(const char * some)

{

* * if(!some)

* * {

* * * *断言(某些);

* * * *返回-1; // -1表示错误

* *}

* * ...

}


什么是你的偏好还是没有?为什么?
hello,
please see following two code snatches:
1.
int foo (const char* some)
{
* * assert(some);
* * if (!some)
* * {
* * * * return -1; //-1 indicates error
* * }
* * ...
}

2.
int foo (const char* some)
{
* * if (!some)
* * {
* * * * assert(some);
* * * * return -1; // -1 indicates error
* * }
* * ...
}

what?s the preference of you or none of them? why?



我通常更喜欢在入场时立即测试功能前置条件




我也相信断言应该留在生产中

代码。否则,您的开发环境不符合您的生产环境,并且您会在测试中引入

风险因素。


如果这样是''关键''代码,无法中止,

然后有更多的理由不引入生产和开发之间的差异


环境。 [换句话说,完全抛弃断言。]

I generally prefer to test function preconditions
immediately on entry.

I also believe that asserts should be left in production
code. Otherwise your development environment is not
matching your production environment and you introduce
an element of risk in testing.

If this is ''critical'' code that cannot aford to abort,
then there''s even more reason not to introduce
differences between production and development
environments. [In other words, dump the asserts altogether.]


我更喜欢第二个,但我无法说服我的朋友。
I prefer the second but I cannot persuade my friends.



你还没说出你为什么喜欢它?


我不认为我不想要通过

整个函数进行筛选,以确保我找到所有

前置条件断言(如果它们发生变化。)

说,断言是有力的东西。如果你更换一个,

你应该全部查看。


-

彼得

You haven''t said why you prefer it?

I don''t think I would like having to sift through the
whole body of a function to make sure I''ve found all
the precondition asserts (should they change.) That
said, asserts are powerful things. If you change one,
you should review them all.

--
Peter


这篇关于一起使用断言和防守的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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