如果条款 [英] if clause

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

问题描述

你好,


我有一个关于设计的问题C中的问题。


2月ACMqueue

(文章:
http://www.acmqueue.org/modules.php?...owpage&pid=364

),KV说:


一个危险的if子句是你要保护的代码
if if并没有真正受到保护。考虑以下伪代码:


0:if(out< 0)

1:return(fileError)

2:

3:if(permission< operator)

4:return(permissionError)

5:

6 :if(data.len()< = 0)

7:return(dataError)

8:

9:写(out) ,data,data.len)

(...)

添加所有if语句的原因是为了保护

程序从有问题的时候调用write()函数,所以代码的结构应该是这样的:


0:if((out> ; = 0)&&(权限> =运算符)&&(data.len()0))

1:write(out,data,data.len)

2://将所有错误条件返回此处。



在我看来,男人可以与后面的代码争论,男人必须复制错误

支票

并且具有两倍相同的代码。您对此代码段有何看法?

感谢您的回复,


rogz

解决方案

rogz写道:


i有关于design的问题C中的问题。


2月ACMqueue

(文章:
http://www.acmqueue.org/modules.php?...owpage&pid=364

),KV说:


一个危险的if子句是你要保护的代码
if if并没有真正受到保护。考虑以下伪代码:


0:if(out< 0)

1:return(fileError)

2:

3:if(permission< operator)

4:return(permissionError)

5:

6 :if(data.len()< = 0)

7:return(dataError)

8:

9:写(out) ,数据,data.len)


(...)

添加所有if语句的原因是为了保护

该程序来自调用write()函数时出现

a问题,所以代码的结构应该是这样的:


0:if ((out> = 0)&&(权限> =运算符)&&(data.len()0))

1:写(输出,数据,数据。 len)

2://将所有错误条件返回此处。



我无法看到原始代码是如何危险的。我倾向于不信任b $ b信任

编程人员认为返回表达式必须以父母为重点。我可能

以显示的形式编写代码。


在我看来,男人可以与后一个代码争论,男人必须复制错误

检查

并且具有两倍相同的代码。您对此代码段有何看法?



谁是男人,这是一个新时代的事情吗?如果我理解你,我会回复。

-

Nick Keighley


< blockquote class =post_quotes>


>在我看来,男人可以与后一个代码争论,男人必须复制错误
支票
两次相同的代码。您对此代码段有何看法?



谁是男人,这是一个新时代的事情吗?如果我了解你,我会回答。



Err ..你应该阅读一个而不是男人。


rogz


Nick Keighley写道:


在我看来,男人可以与后面的代码争论,男人必须复制错误

支票

并且有两次相同的代码。您对此代码段有何看法?



谁是男人,这是一个新时代的事情吗?如果我了解你,我会回答。



当用作代词时,它是'one'的德语。


问候,

Bart。


Hello,

i have a question about "design" issues in C.

In ACMqueue of february
(article here:
http://www.acmqueue.org/modules.php?...owpage&pid=364
), KV says:

A dangerous if clause is one in which the code you want to protect
with the if isn''t really protected. Consider the following pseudocode:

0: if (out < 0)
1: return (fileError)
2:
3: if (permission < operator)
4: return (permissionError)
5:
6: if (data.len() <= 0)
7: return (dataError)
8:
9: write(out, data, data.len)

(...)
The reason that all the if statements were added was to protect
the program from calling the write() function when there was
a problem, so the code should be structured in just that way:

0: if ((out >= 0) && (permission >= operator) && (data.len() 0))
1: write(out, data, data.len)
2: // Put all the error condition returns here.

In my mind, man can argue with the latter code, man has to copy the error
checks
and have twice the same code. What do you thinks about this code snippet?
Thanks for your replies,

rogz

解决方案

rogz wrote:

i have a question about "design" issues in C.

In ACMqueue of february
(article here:
http://www.acmqueue.org/modules.php?...owpage&pid=364
), KV says:

A dangerous if clause is one in which the code you want to protect
with the if isn''t really protected. Consider the following pseudocode:

0: if (out < 0)
1: return (fileError)
2:
3: if (permission < operator)
4: return (permissionError)
5:
6: if (data.len() <= 0)
7: return (dataError)
8:
9: write(out, data, data.len)

(...)
The reason that all the if statements were added was to protect
the program from calling the write() function when there was
a problem, so the code should be structured in just that way:

0: if ((out >= 0) && (permission >= operator) && (data.len() 0))
1: write(out, data, data.len)
2: // Put all the error condition returns here.

I fail to see how the original code was "dangerous". I tend not to
trust
a coder who thinks return expressions have to be parenthasised. I might
write the code in the form shown.

In my mind, man can argue with the latter code, man has to copy the error
checks
and have twice the same code. What do you thinks about this code snippet?

who is "man", is it a new age thing? I''d respond if I understood you.
--
Nick Keighley


>In my mind, man can argue with the latter code, man has to copy the error
checks
and have twice the same code. What do you thinks about this code snippet?


who is "man", is it a new age thing? I''d respond if I understood you.

Err.. You should read "one" instead of "man".

rogz


Nick Keighley wrote:

In my mind, man can argue with the latter code, man has to copy the error
checks
and have twice the same code. What do you thinks about this code snippet?


who is "man", is it a new age thing? I''d respond if I understood you.

It''s German for "one", when used as a pronoun.

Regards,
Bart.


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

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