编码标准,为什么if(boolVar)over if(boolVar == true) [英] Coding Standards, why if ( boolVar ) over if ( boolVar == true )

查看:42
本文介绍了编码标准,为什么if(boolVar)over if(boolVar == true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览C#/ .NET

的各种编码标准文档,所有人都说要使用:


if(boolVar ){...}而不是if(boolVar == true){...}


对于我的生活,我无法弄明白为什么。如果有的话,我会猜测

对面会更好用,因为!操作员很难阅读。


快速浏览一下,


如果(boolVar == false)比if(!)更容易理解boolVar)


任何人都有一些更具体的理由说明为什么一个人比另一个人更受欢迎

除了一个人少打字。


谢谢,

-A

I''ve been flipping through various coding standards documents for C#/.NET
and all of them say to use:

if ( boolVar ) {...} instead of if ( boolVar == true ) {...}

For the life of my I can''t figure out why. If anything, I would guess the
opposite would be better to use since the ! operator is so hard to read.

Upon quick glance,

if ( boolVar == false ) is easier to understand than if ( !boolVar )

Anybody have some more concrete reasons why one is preferred over the other
aside from one being less typing.

Thanks,
-A

推荐答案

Alfred:

这可以沦为圣战。一般来说,我建议尽可能简单地保持

。想想看看这个代码3个月从现在开始,在凌晨2点,在截止日期之后的15个小时工作2个月后,

直接。


8 - )......去过那里......


John

" Alfred Taylor" < OM *** @ R-E-M-O-V-Eyahoo.com>在消息中写道

news:Ou ************** @ tk2msftngp13.phx.gbl ...
Alfred:

This can degenerate into a holy war. In general, I would recommend keeping
it as simple as possible. Think about looking at this code 3 months from
now, at 2:00 AM under a deadline after working 15 hour days for 2 months
straight.

8-)... Been there...

John
"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
我去过翻阅C#/ .NET的各种编码标准文件,并且所有人都说要使用:

if(boolVar){...}而不是if(boolVar == true){ ......}

对于我的生活,我无法弄清楚为什么。如果有的话,我会猜测
对面会更好用,因为!操作员很难阅读。

快速浏览一下,

if(boolVar == false)比(!boolVar)
任何人都有一些更具体的理由说明为什么一个人优先于
,除了一个人少打字。

谢谢,
-A
I''ve been flipping through various coding standards documents for C#/.NET
and all of them say to use:

if ( boolVar ) {...} instead of if ( boolVar == true ) {...}

For the life of my I can''t figure out why. If anything, I would guess the
opposite would be better to use since the ! operator is so hard to read.

Upon quick glance,

if ( boolVar == false ) is easier to understand than if ( !boolVar )

Anybody have some more concrete reasons why one is preferred over the other aside from one being less typing.

Thanks,
-A



这是我调用编程风格的#1规则的地方。 :)


"编写一个平庸标准的代码优于那些

不标准的优秀代码。


或者换句话说:


如果其他人都采用标准方式,那么你的聪明解决方案就不再是
了聪明,除非它提供超过标准的惊人的

优势。


如果业内人士都在写作


if(boolVar == true)


而我是写的


if(boolVar)


声称它是更好的因为我不得不输入更少的字符,我不会责怪我的同事,如果他们要给我结婚,那么所有的楔子都会把我扔到街上。每当他们阅读我的代码时,他们就必须精神上停下来思考我b
$ b做了什么。


反过来也是如此。


几乎每个C / C ++ / Java / C#程序员都写了


if(boolVar)





if(!boolVar)


这个事实本身就是替代品语法less

可读。
This is where I invoke my #1 rule of programming style. :)

"Code written to a mediocre standard is better than brilliant code that
is not standard."

or, stated another way:

"If everyone else is doing it a standard way, your clever solution is
by definition no longer clever, unless it offers some stunning
advantage over the standard."

If everyone in the industry were writing

If (boolVar == true)

and I were to write instead

if (boolVar)

claiming that it was "better" because I had to type fewer characters, I
would not blame my colleagues if they were to give me the wedgie to end
all wedgies and tossed me out on the street. Every time they were to
read my code they would have to mentally stop and think about what I
was doing.

The reverse is also true.

Almost every C / C++ / Java / C# programmer out there writes

if (boolVar)

and

if (!boolVar)

that fact, in and of itself, makes the alternative syntax "less
readable".
从个人经验来看,我可以说阅读
From personal experience, I can say that reading




if(boolVar == false)


对我而言就像一个减速带。我已经读了C 20年了,而且我已经习惯了b $ b。实际上,它变得更具可读性。对我来说。



if (boolVar == false)

is like a speed bump for me. I''ve been reading C for 20 years and I''m
used to the standard. It has, in effect, become "more readable" for me.


嗨阿尔弗雷德,


" Alfred Taylor" < OM *** @ R-E-M-O-V-Eyahoo.com>在消息中写道

news:Ou ************** @ tk2msftngp13.phx.gbl ...
Hi Alfred,

"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
我去过翻阅C#/ .NET的各种编码标准文件,并且所有人都说要使用:

if(boolVar){...}而不是if(boolVar == true){ ......}

对于我的生活,我无法弄清楚为什么。如果有的话,我会猜测
对面会更好用,因为!操作员很难阅读。

快速浏览一下,

if(boolVar == false)比(!boolVar)
任何人都有一些更具体的理由说明为什么一个人优先于
,而另一个人更少打字。
I''ve been flipping through various coding standards documents for C#/.NET
and all of them say to use:

if ( boolVar ) {...} instead of if ( boolVar == true ) {...}

For the life of my I can''t figure out why. If anything, I would guess the
opposite would be better to use since the ! operator is so hard to read.

Upon quick glance,

if ( boolVar == false ) is easier to understand than if ( !boolVar )

Anybody have some more concrete reasons why one is preferred over the other aside from one being less typing.




简而言之,它可以防止出现语法错误成为逻辑错误。它的价格并不罕见,特别是对于过去没有使用过C /变量语言的人来说,意外地使用赋值运算符(=)代替

等于运算符(==)。考虑一下这个C#代码:


int i = 0;

...

if(i = 3){.. 。$

上面的代码会被标记为编译时错误,因为

赋值运算符(=)返回赋值,在这种情况下是3,

这不是布尔值。另一方面,如果你这样做:


bool boolVar = false;

...

if(boolVar = true){...}


上面的代码不会导致编译时错误,即使它可能不是预期的b $ b。赋值运算符(=)返回分配的

值,在这种情况下为true,*是*布尔值。


如果你从不使用" == true"或== false这个错误是可以避免的。


问候,

Daniel



In short, it prevents syntax errors from becoming logical errors. It''s
not unusual, especially for people who haven''t used C-variant languages in
the past, to accidentally use the assignment operator (=) instead of the
equality operator (==). Consider this C# code:

int i = 0;
...
if (i = 3) {...}

The above code would be flagged as a compile-time error because the
assignment operator (=) returns the value assigned, which in this case is 3,
which is not a boolean value. On the other hand, if you do this:

bool boolVar = false;
...
if (boolVar = true) {...}

The above code will not cause a compile time error, even though it''s
probably not what was intended. The assignment operator (=) returns the
value assigned, which in this case is true, which *is* a boolean value.

If you never use "== true" or "== false" the mistake can be avoided.

Regards,
Daniel


这篇关于编码标准,为什么if(boolVar)over if(boolVar == true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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