将常数置于某个条件下的最佳位置在哪里?

时间:2021-04-17 22:52:46

Where is the best place to put the constant in a condition? Left side or Right side?

将常数置于某个条件下的最佳位置在哪里?左侧还是右侧?

I personally use in the right side:

我个人在右边使用:

if($value > 23)
{
}

8 个解决方案

#1


The right side. The left side is a tradition in C/C++ because people sometimes forget and uses "=" instead of "==" and putting the const on the left side causes a compilation error in this case.

右边。左侧是C / C ++中的传统,因为人们有时会忘记并使用“=”而不是“==”并且将const放在左侧会导致编译错误。

#2


A lot of people will say the LHS because it prevents you doing subtle and damaging things like if (foo = KBAR) (note the lack of '==') but I always find that jarring for readability.

很多人会说LHS,因为它会阻止你做一些微妙的和破坏性的事情,比如if(foo = KBAR)(注意缺少'=='),但我总觉得这很容易让人感到震惊。

#3


depends:

if (23 <= i and i <= 40)

if(23 <= i且i <= 40)

but i would prefer right side, it reads more naturallly

但我更喜欢右侧,它更自然地阅读

#4


Put the condition on the right side, since that's the "natural" place, and rely on your compiler to generate a warning if you accidentally use = instead of ==.

将条件放在右侧,因为这是“自然”的地方,并且如果您不小心使用=而不是==,则依赖编译器生成警告。

#5


Does it really matter? It may help you if you keep a convention, but whether it is to keep the constants on one side, or to always use the <, <= operators and avoid the >, >=; that really depends on you.

真的有关系吗?如果你保持一个约定,它可能会有所帮助,但它是保持常量在一边,还是总是使用<,<=运算符并避免>,> =;这真的取决于你。

It surely doesn't matter to the compiler/interpreter, and modern compilers should give a clear warning when you accidentally write "set to" (=) instead of "does it equal" (==), as pointed out in ocdecio's post.

这对编译器/解释器来说无关紧要,现代编译器应该在你不小心写“set to”(=)而不是“is it equal”(==)时给出一个明确的警告,正如ocdecio的帖子所指出的那样。

#6


The answer, as always, is "it depends". In most cases, it reads more naturally to put it on the right, as in the OP's example. In other cases, particularly compound statements that check to see if something is in a range (see Peter Miehle's example), it can go either way. I think you should use whichever makes the statement clearer to any future programmers who happen across your code. If there is no clear difference in readability, I recommend defaulting to putting it on the right, since that is what most people expect (principle of least surprise). As many have mentioned already, any decent compiler nowadays will warn you if you attempt to perform an assignment inside an if statement (you can usually silence this warning by putting an extra set of parentheses around the assignment). Also, it has been mentioned that some JIT or interpreted languages might make it hard to find this problem without the constant-on-the-left trick, but IIRC, many of them will also emit a warning in this case, so if you run them with warnings treated as errors, it will help you catch that problem.

答案一如既往地是“它取决于”。在大多数情况下,它更自然地将它放在右边,就像在OP的例子中那样。在其他情况下,特别是复合语句,检查某些东西是否在某个范围内(参见Peter Miehle的例子),它可以采用任何一种方式。我认为你应该使用任何一个使得声明更清晰的任何未来程序员在你的代码中发生。如果可读性没有明显差异,我建议默认将它放在右边,因为这是大多数人所期望的(最少惊喜的原则)。正如许多人已经提到的,如果你试图在if语句中执行赋值,现在任何体面的编译器都会发出警告(你通常可以通过在赋值周围添加一组额外的括号来使这个警告静音)。此外,有人提到一些JIT或解释型语言可能会在没有左边的常量技巧的情况下很难找到这个问题,但是IIRC,其中许多也会在这种情况下发出警告,所以如果你运行他们将警告视为错误,它将帮助您解决问题。

#7


I prefer left side, as it prevents accidental assignments like so:

我更喜欢左侧,因为它可以防止意外分配,如下所示:

// when using = instead of == this can result in accidental assignment
if ($value == null) {}

// $value cannot be accidentally assigned this way 
if (null === $value) {}

NOTE: from reading other answer I understand when using compiled languages, this could get you in trouble. I still prefer using this, as my main language is PHP. For compiled languages, please refer to the answers others have already given.

注意:从阅读其他答案我明白使用编译语言时,这可能会让你遇到麻烦。我仍然喜欢使用它,因为我的主要语言是PHP。对于编译语言,请参考其他人已经给出的答案。

#8


Always use < (and <=), never use > (or >=), and avoid languages which can't distinguish between assignment and equality.

始终使用<(和<=),从不使用>(或> =),并避免使用无法区分赋值和相等的语言。

First part of the rule means that numbers in your conditions occur in their usual order, smallest on the left, largest on the right. This is a great help when your conditions contain multiple terms (eg 3<x && x<=14)

规则的第一部分意味着您的条件中的数字按通常顺序出现,左侧最小,右侧最大。当您的条件包含多个术语时(例如3

Second part of the rule means letting a compiler sweat about things a compiler is good at (such as spotting how many ===== signs you've typed).

规则的第二部分意味着让编译器对编译器擅长的事情感到厌烦(例如发现你输入了多少个=====符号)。

And I make these assertions forcefully and positively sure and certain in the knowledge that this is only my opinion and that there isn't a right or wrong answer.

而且我确信这些断言是有力而肯定的,并且确定这只是我的意见,并且没有正确或错误的答案。

Regards

#1


The right side. The left side is a tradition in C/C++ because people sometimes forget and uses "=" instead of "==" and putting the const on the left side causes a compilation error in this case.

右边。左侧是C / C ++中的传统,因为人们有时会忘记并使用“=”而不是“==”并且将const放在左侧会导致编译错误。

#2


A lot of people will say the LHS because it prevents you doing subtle and damaging things like if (foo = KBAR) (note the lack of '==') but I always find that jarring for readability.

很多人会说LHS,因为它会阻止你做一些微妙的和破坏性的事情,比如if(foo = KBAR)(注意缺少'=='),但我总觉得这很容易让人感到震惊。

#3


depends:

if (23 <= i and i <= 40)

if(23 <= i且i <= 40)

but i would prefer right side, it reads more naturallly

但我更喜欢右侧,它更自然地阅读

#4


Put the condition on the right side, since that's the "natural" place, and rely on your compiler to generate a warning if you accidentally use = instead of ==.

将条件放在右侧,因为这是“自然”的地方,并且如果您不小心使用=而不是==,则依赖编译器生成警告。

#5


Does it really matter? It may help you if you keep a convention, but whether it is to keep the constants on one side, or to always use the <, <= operators and avoid the >, >=; that really depends on you.

真的有关系吗?如果你保持一个约定,它可能会有所帮助,但它是保持常量在一边,还是总是使用<,<=运算符并避免>,> =;这真的取决于你。

It surely doesn't matter to the compiler/interpreter, and modern compilers should give a clear warning when you accidentally write "set to" (=) instead of "does it equal" (==), as pointed out in ocdecio's post.

这对编译器/解释器来说无关紧要,现代编译器应该在你不小心写“set to”(=)而不是“is it equal”(==)时给出一个明确的警告,正如ocdecio的帖子所指出的那样。

#6


The answer, as always, is "it depends". In most cases, it reads more naturally to put it on the right, as in the OP's example. In other cases, particularly compound statements that check to see if something is in a range (see Peter Miehle's example), it can go either way. I think you should use whichever makes the statement clearer to any future programmers who happen across your code. If there is no clear difference in readability, I recommend defaulting to putting it on the right, since that is what most people expect (principle of least surprise). As many have mentioned already, any decent compiler nowadays will warn you if you attempt to perform an assignment inside an if statement (you can usually silence this warning by putting an extra set of parentheses around the assignment). Also, it has been mentioned that some JIT or interpreted languages might make it hard to find this problem without the constant-on-the-left trick, but IIRC, many of them will also emit a warning in this case, so if you run them with warnings treated as errors, it will help you catch that problem.

答案一如既往地是“它取决于”。在大多数情况下,它更自然地将它放在右边,就像在OP的例子中那样。在其他情况下,特别是复合语句,检查某些东西是否在某个范围内(参见Peter Miehle的例子),它可以采用任何一种方式。我认为你应该使用任何一个使得声明更清晰的任何未来程序员在你的代码中发生。如果可读性没有明显差异,我建议默认将它放在右边,因为这是大多数人所期望的(最少惊喜的原则)。正如许多人已经提到的,如果你试图在if语句中执行赋值,现在任何体面的编译器都会发出警告(你通常可以通过在赋值周围添加一组额外的括号来使这个警告静音)。此外,有人提到一些JIT或解释型语言可能会在没有左边的常量技巧的情况下很难找到这个问题,但是IIRC,其中许多也会在这种情况下发出警告,所以如果你运行他们将警告视为错误,它将帮助您解决问题。

#7


I prefer left side, as it prevents accidental assignments like so:

我更喜欢左侧,因为它可以防止意外分配,如下所示:

// when using = instead of == this can result in accidental assignment
if ($value == null) {}

// $value cannot be accidentally assigned this way 
if (null === $value) {}

NOTE: from reading other answer I understand when using compiled languages, this could get you in trouble. I still prefer using this, as my main language is PHP. For compiled languages, please refer to the answers others have already given.

注意:从阅读其他答案我明白使用编译语言时,这可能会让你遇到麻烦。我仍然喜欢使用它,因为我的主要语言是PHP。对于编译语言,请参考其他人已经给出的答案。

#8


Always use < (and <=), never use > (or >=), and avoid languages which can't distinguish between assignment and equality.

始终使用<(和<=),从不使用>(或> =),并避免使用无法区分赋值和相等的语言。

First part of the rule means that numbers in your conditions occur in their usual order, smallest on the left, largest on the right. This is a great help when your conditions contain multiple terms (eg 3<x && x<=14)

规则的第一部分意味着您的条件中的数字按通常顺序出现,左侧最小,右侧最大。当您的条件包含多个术语时(例如3

Second part of the rule means letting a compiler sweat about things a compiler is good at (such as spotting how many ===== signs you've typed).

规则的第二部分意味着让编译器对编译器擅长的事情感到厌烦(例如发现你输入了多少个=====符号)。

And I make these assertions forcefully and positively sure and certain in the knowledge that this is only my opinion and that there isn't a right or wrong answer.

而且我确信这些断言是有力而肯定的,并且确定这只是我的意见,并且没有正确或错误的答案。

Regards