2个问题 [英] 2 style questions
问题描述
1.
我正和某人讨论,我认为这只是宗教信仰。
我们正在开发一些软件,他正在查看我的代码。
我使用if(指针)来测试NULL的指针。他说必须是
(p!= NULL)。
参数是它更具可读性,如果(p)不可移植。
我说第一个是样式问题,我认为如果(p)更可读
并且第二个是完全错误的。
那么,他对可移植性问题是对的吗?是否(p)不严格正确
C代码?
2.
然后我开始查看他的代码和什么我注意到他非常
经常做这样的事情:
char * func(char ** out)
{
* out = malloc(100);
返回malloc(10);
}
这也可能是一个风格问题,但它是一种风格,它很容易引入错误(实际上我发现了一些)
甚至如果您不知道
函数的内部结构,则更容易引入错误。那么,您如何看待这种风格?
1.
I was having a discussion with somebody and I think it''s just religious.
We are developing some software and he was looking through my code.
I use if (pointer) to test a pointer for NULL. He says it must be if
(p!=NULL).
The arguments are that it''s more readable and that if (p) is not portable.
I said the first is a style issue and that I think if(p) is more readable
and that the second is plain wrong.
So, is he right about the portability issue? is if (p) not strictly correct
C code?
2.
Then I started looking through his code and what I noticed that he very
often does things like this:
char *func(char **out)
{
*out = malloc(100);
return malloc(10);
}
This might also be a style issue, but it''s a style where it''s very easy to
introduce bugs (and indeed I found a few)
and even more easy to introduce bugs if you don''t know the internals of the
function. So, what do you think about this style?
推荐答案
" Sander" < i@bleat.nospam.com>写道:
"Sander" <i@bleat.nospam.com> writes:
1.
我正在与某人进行讨论,我认为这只是宗教信仰。
我们正在开发一些软件,他正在寻找通过我的代码。
我使用if(指针)来测试NULL的指针。他说必须是
(p!= NULL)。
这两个测试是等价的。
参数是它更具可读性,如果(p)不可移植。
后者不是这样的:`if(p)''对于任何指针来说都相当于`if(p
!= NULL)'' p。
2.
然后我开始查看他的代码以及我注意到他经常做这样的事情:
char * func(char ** out)
{
* out = malloc(100);
返回malloc(10);
}
这也可能是一个风格问题,但它是一种风格,它很容易引入错误(事实上我发现了一些)
1.
I was having a discussion with somebody and I think it''s just religious.
We are developing some software and he was looking through my code.
I use if (pointer) to test a pointer for NULL. He says it must be if
(p!=NULL).
These two tests are equivalent.
The arguments are that it''s more readable and that if (p) is
not portable.
The latter is not true: `if (p)'' is portably equivalent to `if (p
!= NULL)'' for any pointer p.
2.
Then I started looking through his code and what I noticed that he very
often does things like this:
char *func(char **out)
{
*out = malloc(100);
return malloc(10);
}
This might also be a style issue, but it''s a style where it''s very easy to
introduce bugs (and indeed I found a few)
它至少看起来很可疑。
-
大笔资金倾向于解除我可能遇到的任何顾忌。
- - Stephan Wilms
It at least looks suspicious.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Sander写道:
Sander wrote:
1.
我正和某人讨论,我认为只是虔诚的。
我们正在开发一些软件,他正在查看我的代码。
我使用if(指针)来测试一个指针为NULL。他说必须是
(p!= NULL)。
我更喜欢***后者,if(p!= NULL)
参数是它更具可读性
我同意这一点。
和if(p)不可携带。
然而,这是无稽之谈。
我说第一个是样式问题,我认为如果(p)更具可读性
而第二个是完全错误的。
你是对的。我不同意你的风格选择,但它只是一种风格
选择。
那么,他对可移植性问题是否正确?
不,但他对风格选择是正确的。 :-)
是否(p)不严格正确的C代码?
没关系。
断言(p)但是,不是(除非你有C99编译器)。 />
2.
然后我开始查看他的代码以及我注意到他经常做这样的事情:
char * func(char * * out)
{out /> * out = malloc(100);
返回malloc(10);
}
EEK!这应该做什么?
这也可能是一个风格问题,但它是一种非常容易引入错误的风格(事实上我发现了一些如果你不知道这个功能的内部结构,那么就更容易引入错误了。所以,你怎么看待这种风格?
1.
I was having a discussion with somebody and I think it''s just religious.
We are developing some software and he was looking through my code.
I use if (pointer) to test a pointer for NULL. He says it must be if
(p!=NULL).
I ***prefer*** the latter, if(p != NULL)
The arguments are that it''s more readable
I agree with this.
and that if (p) is not portable.
This, however, is nonsense.
I said the first is a style issue and that I think if(p) is more readable
and that the second is plain wrong.
You are correct. I disagree with your style choice, but it is simply a style
choice.
So, is he right about the portability issue?
No. But he''s right about the style choice. :-)
is if (p) not strictly
correct C code?
It''s fine.
assert(p), however, is not (unless you have a C99 compiler).
2.
Then I started looking through his code and what I noticed that he very
often does things like this:
char *func(char **out)
{
*out = malloc(100);
return malloc(10);
}
EEK! What is this supposed to do?
This might also be a style issue, but it''s a style where it''s very easy to
introduce bugs (and indeed I found a few)
and even more easy to introduce bugs if you don''t know the internals of
the function. So, what do you think about this style?
我认为这是可怕的。具体来说,我认为你向我们展示的func()是可怕的。我碰巧同意if(p!= NULL)是一种非常明智的方式
来呈现代码。我更喜欢保留if(x)的情况,其中x很明显是布尔意图,例如
int Split(vector * v,char * s, int WantDelimiters)
{
char * left = s;
char * right = left;
while( *左)
{
if(WantDelimiters)
{
等
-
Richard Heathfield: bi****@eton.powernet.co .uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
I think it''s ghastly. Specifically, I think the func() you showed us is
ghastly. I happen to agree with if(p != NULL) as being a very sensible way
to present the code. I prefer to reserve if(x) for situations where x is
clearly of Boolean intent, e.g.
int Split(vector *v, char *s, int WantDelimiters)
{
char *left = s;
char *right = left;
while(*left)
{
if(WantDelimiters)
{
etc
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
*这样说起Sander< i@bleat.nospam.com>:
你好,
* Thus spoke Sander <i@bleat.nospam.com>:
Hallo,
我使用if(指针)来测试NULL的指针。他说必须是
(p!= NULL)。
不过。但出于可读性原因,他可能更喜欢它。很多程序员
我知道它认为它是一种很好的编程风格。
(我的标准版本现在不可用,但是:)
| C中的每个指针类型都有一个特殊值,称为/ null指针/,
|这与该类型的每个有效指针不同,后者比较了
|等于空指针常量,它转换为空指针
|其他指针类型,其值为false用于
|时布尔上下文。 / null指针常量在C中是任何整数
|值为0的常量表达式,或者这样的表达式转换为类型
| void *。
(Harbinson / Steele)
参数是它更具可读性,如果(p)不可移植。
我用?if(!p)?如果(p ==(void *)0),if(p == int *)0),你可以进一步遇到喜欢使用
的程序员。 。
我说第一个是样式问题,我认为如果(p)更具可读性,那么第二个是完全错误的。
正如我之前所说,我也喜欢速记符号,但可能会有更多人在这一点上不同意。
char * func(char ** out)
{
* out = malloc(100);
返回malloc(10);
}
所以,您如何看待这种风格?
I use if (pointer) to test a pointer for NULL. He says it must be if
(p!=NULL).
No. But he might prefer it for readability reasons. Many programmers
that I know consider it as good programming style.
(My version of the standard is not available right now, but:)
| Every pointer type in C has a special value called a /null pointer/,
| which is different from every valid pointer of that type, which compares
| equal to a null pointer constant, which converts to the null pointers of
| other pointer types, and which has the value "false" when used in a
| boolean context. The /null pointer constant" in C is any integer
| constant expression with the value 0, or such an expression cast to type
| void*.
(Harbinson/Steele)
The arguments are that it''s more readable and that if (p) is not portable.
I use ?if(!p)? and you may further meet programmers that like to use
if(p==0), if(p==(void*)0), if(p== int*)0)...
I said the first is a style issue and that I think if(p) is more readable
and that the second is plain wrong.
As I said before I prefer the shorthand notation too, but there might be
more people that disagree on this point.
char *func(char **out)
{
*out = malloc(100);
return malloc(10);
}
So, what do you think about this style?
No.
Wolfgang。
-
" Erfahrungen - das sind die vernarbten Wunden unserer Dummheit。"
- John Osborne
No.
Wolfgang.
--
"Erfahrungen -- das sind die vernarbten Wunden unserer Dummheit."
-- John Osborne
这篇关于2个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!