Given the following idioms:
鉴于以下习语:
1)
variable = value1
if condition
variable = value2
2)
variable = value2
if not condition
variable = value1
3)
if condition
variable = value2
else
variable = value1
4)
if not condition
variable = value1
else
variable = value2
Which do you prefer, and why?
你更喜欢哪个?为什么?
We assume the most common execution path to be that of condition
being false.
我们假设最常见的执行路径是条件为false。
I tend to learn towards using 1), although I'm not exactly sure why I like it more.
我倾向于学习使用1),虽然我不确定为什么我更喜欢它。
Note: The following examples may be simpler—and thus possibly more readable—but not all languages provide such syntax, and they are not suitable for extending the variable assignment to include more than one statement in the future.
注意:以下示例可能更简单 - 因此可能更具可读性 - 但并非所有语言都提供此类语法,并且它们不适合将变量赋值扩展为包含将来的多个语句。
variable = condition ? value2 : value1
...
variable = value2 if condition else value1
12 个解决方案
#1
10
In theory, I prefer #3 as it avoids having to assign a value to the variable twice. In the real world though I use any of the four above that would be more readable or would express more clearly my intention.
从理论上讲,我更喜欢#3,因为它避免了必须为变量赋值两次。在现实世界中,虽然我使用上述四种中的任何一种,它们更具可读性,或者更清楚地表达我的意图。
#2
4
I prefer method 3 because it is more concise and a logical unit. It sets the value only once, it can be moved around as a block, and it's not that error-prone (which happens, esp. in method 1 if setting-to-value1 and checking-and-optionally-setting-to-value2 are separated by other statements)
我更喜欢方法3,因为它更简洁,更合乎逻辑。它只设置一次值,它可以作为一个块移动,并且它不是容易出错的(这种情况发生,特别是在方法1中,如果设置为value1并且check-and-optional-setting-to-value2被其他陈述分开)
#3
3
3) is the clearest expression of what you want to happen. I think all the others require some extra thinking to determine which value is going to end up in the variable.
3)是你想要发生的事情的最清晰的表达。我认为所有其他人都需要一些额外的思考来确定哪个值最终会在变量中结束。
In practice, I would use the ternary operator (?:) if I was using a language that supported it. I prefer to write in functional or declarative style over imperative whenever I can.
在实践中,如果我使用支持它的语言,我会使用三元运算符(?:)。我愿意尽我所能在功能性或陈述性风格上写作。
#4
1
I tend to use #1 alot myself. if condition
reads easier than if !condition
, especially if you acidentally miss the '!', atleast to my mind atleast.
我倾向于自己使用#1。如果条件读取比if条件更容易,特别是如果你偶然错过'!',至少在我的脑海里。
Most coding I do is in C#, but I still tend to steer clear of the terniary operator, unless I'm working with (mostly) local variables. Lines tend to get long VERY quickly in a ternary operator if you're calling three layers deep into some structure, which quickly decreases the readability again.
我做的大多数编码都是在C#中,但我仍然倾向于避开三元运算符,除非我正在使用(主要是)局部变量。如果你在一些结构中调用三层深度,那么线在三元运算符中很快就会变得非常快,这会很快再次降低可读性。
#5
1
Note: The following examples may be simpler—and thus possibly more readable—but not all languages provide such syntax
注意:以下示例可能更简单 - 因此可能更具可读性 - 但并非所有语言都提供此类语法
This is no argument for not using them in languages that do provide such a syntax. Incidentally, that includes all current mainstream languages after my last count.
这不是在提供这种语法的语言中不使用它们的论据。顺便提一下,这包括我上次计算后的所有当前主流语言。
and they are not suitable for extending the variable assignment to include more than one statement in the future.
并且它们不适合将变量赋值扩展为包含将来的多个语句。
This is true. However, it's often certain that such an extension will absolutely never take place because the condition
will always yield one of two possible cases.
这是真的。然而,通常肯定这样的扩展绝对不会发生,因为条件总是会产生两种可能的情况之一。
In such situations I will always prefer the expression variant over the statement variant because it reduces syntactic clutter and improves expressiveness. In other situations I tend to go with the switch
statement mentioned before – if the language allows this usage. If not, fall-back to generic if
.
在这种情况下,我总是更喜欢表达式变体而不是语句变体,因为它减少了语法杂乱并提高了表达能力。在其他情况下,我倾向于使用前面提到的switch语句 - 如果语言允许这种用法。如果没有,请回退到通用if。
#6
0
switch
statement also works. If it's simple and more than 2 or 3 options, that's what I use.
switch语句也有效。如果它很简单,超过2或3个选项,那就是我使用的。
#7
0
In a situation where the condition might not happen. I would go with 1 or 2. Otherwise its just based on what i want the code to do. (ie. i agree with cruizer)
在可能不会发生这种情况的情况下。我会选择1或2.否则它只是基于我想要的代码。 (即我同意巡洋舰)
#8
0
I tend to use if not...return.
我倾向于使用,如果不...返回。
But that's if you are looking to return a variable. Getting disqualifiers out of the way first tends to make it more readable. It really depends on the context of the statement and also the language. A case statement might work better and be readable most of the time, but performance suffers under VB so a series of if/else statements makes more sense in that specific case.
但是,如果你想要返回一个变量。首先使取消资格者脱离困境往往会使其更具可读性。这实际上取决于声明的语境和语言。 case语句可能在大多数情况下更好地工作并且可读,但是在VB下性能会受到影响,所以在这种特定情况下,一系列if / else语句更有意义。
#9
0
Method 1 or method 3 for me. Method 1 can avoid an extra scope entrance/exit, but method 3 avoids an extra assignment. I'd tend to avoid Method 2 as I try to keep condition logic as simple as possible (in this case, the ! is extraneous as it could be rewritten as method 1 without it) and the same reason applies for method 4.
方法1或方法3对我来说。方法1可以避免额外的范围入口/出口,但方法3避免额外的分配。我倾向于避免方法2,因为我试图保持条件逻辑尽可能简单(在这种情况下,!是无关紧要的,因为它可以在没有它的情况下重写为方法1),同样的原因适用于方法4。
#10
0
It depends on what the condition is I'm testing.
这取决于我正在测试的条件。
If it's an error flag condition then I'll use 1) setting the Error flag to catch the error and then if the condition is successfull clear the error flag. That way there's no chance of missing an error condition.
如果它是一个错误标志条件,那么我将使用1)设置错误标志来捕获错误,然后如果条件成功,则清除错误标志。这样就没有错过错误的可能性。
For everything else I'd use 3)
对于我使用的其他一切3)
The NOT logic just adds to confusion when reading the code - well in my head, can't speak for eveyone else :-)
NOT逻辑只会在阅读代码时增加混乱 - 在我脑海中,不能代表eveyone :-)
#11
0
If the variable has a natural default value I would go with #1. If either value is equally (in)appropriate for a default then I would go with #2.
如果变量具有自然默认值,我将使用#1。如果任何一个值等于(默认)适合默认值,那么我将使用#2。
#12
0
It depends. I like the ternary operators, but sometimes it's clearer if you use an 'if' statement. Which of the four alternatives you choose depends on the context, but I tend to go for whichever makes the code's function clearer, and that varies from situation to situation.
这取决于。我喜欢三元运算符,但有时候如果使用'if'语句则更清楚。您选择的四种替代方案中的哪一种取决于上下文,但我倾向于选择哪种方式使代码的功能更加清晰,并且因情况而异。
#1
10
In theory, I prefer #3 as it avoids having to assign a value to the variable twice. In the real world though I use any of the four above that would be more readable or would express more clearly my intention.
从理论上讲,我更喜欢#3,因为它避免了必须为变量赋值两次。在现实世界中,虽然我使用上述四种中的任何一种,它们更具可读性,或者更清楚地表达我的意图。
#2
4
I prefer method 3 because it is more concise and a logical unit. It sets the value only once, it can be moved around as a block, and it's not that error-prone (which happens, esp. in method 1 if setting-to-value1 and checking-and-optionally-setting-to-value2 are separated by other statements)
我更喜欢方法3,因为它更简洁,更合乎逻辑。它只设置一次值,它可以作为一个块移动,并且它不是容易出错的(这种情况发生,特别是在方法1中,如果设置为value1并且check-and-optional-setting-to-value2被其他陈述分开)
#3
3
3) is the clearest expression of what you want to happen. I think all the others require some extra thinking to determine which value is going to end up in the variable.
3)是你想要发生的事情的最清晰的表达。我认为所有其他人都需要一些额外的思考来确定哪个值最终会在变量中结束。
In practice, I would use the ternary operator (?:) if I was using a language that supported it. I prefer to write in functional or declarative style over imperative whenever I can.
在实践中,如果我使用支持它的语言,我会使用三元运算符(?:)。我愿意尽我所能在功能性或陈述性风格上写作。
#4
1
I tend to use #1 alot myself. if condition
reads easier than if !condition
, especially if you acidentally miss the '!', atleast to my mind atleast.
我倾向于自己使用#1。如果条件读取比if条件更容易,特别是如果你偶然错过'!',至少在我的脑海里。
Most coding I do is in C#, but I still tend to steer clear of the terniary operator, unless I'm working with (mostly) local variables. Lines tend to get long VERY quickly in a ternary operator if you're calling three layers deep into some structure, which quickly decreases the readability again.
我做的大多数编码都是在C#中,但我仍然倾向于避开三元运算符,除非我正在使用(主要是)局部变量。如果你在一些结构中调用三层深度,那么线在三元运算符中很快就会变得非常快,这会很快再次降低可读性。
#5
1
Note: The following examples may be simpler—and thus possibly more readable—but not all languages provide such syntax
注意:以下示例可能更简单 - 因此可能更具可读性 - 但并非所有语言都提供此类语法
This is no argument for not using them in languages that do provide such a syntax. Incidentally, that includes all current mainstream languages after my last count.
这不是在提供这种语法的语言中不使用它们的论据。顺便提一下,这包括我上次计算后的所有当前主流语言。
and they are not suitable for extending the variable assignment to include more than one statement in the future.
并且它们不适合将变量赋值扩展为包含将来的多个语句。
This is true. However, it's often certain that such an extension will absolutely never take place because the condition
will always yield one of two possible cases.
这是真的。然而,通常肯定这样的扩展绝对不会发生,因为条件总是会产生两种可能的情况之一。
In such situations I will always prefer the expression variant over the statement variant because it reduces syntactic clutter and improves expressiveness. In other situations I tend to go with the switch
statement mentioned before – if the language allows this usage. If not, fall-back to generic if
.
在这种情况下,我总是更喜欢表达式变体而不是语句变体,因为它减少了语法杂乱并提高了表达能力。在其他情况下,我倾向于使用前面提到的switch语句 - 如果语言允许这种用法。如果没有,请回退到通用if。
#6
0
switch
statement also works. If it's simple and more than 2 or 3 options, that's what I use.
switch语句也有效。如果它很简单,超过2或3个选项,那就是我使用的。
#7
0
In a situation where the condition might not happen. I would go with 1 or 2. Otherwise its just based on what i want the code to do. (ie. i agree with cruizer)
在可能不会发生这种情况的情况下。我会选择1或2.否则它只是基于我想要的代码。 (即我同意巡洋舰)
#8
0
I tend to use if not...return.
我倾向于使用,如果不...返回。
But that's if you are looking to return a variable. Getting disqualifiers out of the way first tends to make it more readable. It really depends on the context of the statement and also the language. A case statement might work better and be readable most of the time, but performance suffers under VB so a series of if/else statements makes more sense in that specific case.
但是,如果你想要返回一个变量。首先使取消资格者脱离困境往往会使其更具可读性。这实际上取决于声明的语境和语言。 case语句可能在大多数情况下更好地工作并且可读,但是在VB下性能会受到影响,所以在这种特定情况下,一系列if / else语句更有意义。
#9
0
Method 1 or method 3 for me. Method 1 can avoid an extra scope entrance/exit, but method 3 avoids an extra assignment. I'd tend to avoid Method 2 as I try to keep condition logic as simple as possible (in this case, the ! is extraneous as it could be rewritten as method 1 without it) and the same reason applies for method 4.
方法1或方法3对我来说。方法1可以避免额外的范围入口/出口,但方法3避免额外的分配。我倾向于避免方法2,因为我试图保持条件逻辑尽可能简单(在这种情况下,!是无关紧要的,因为它可以在没有它的情况下重写为方法1),同样的原因适用于方法4。
#10
0
It depends on what the condition is I'm testing.
这取决于我正在测试的条件。
If it's an error flag condition then I'll use 1) setting the Error flag to catch the error and then if the condition is successfull clear the error flag. That way there's no chance of missing an error condition.
如果它是一个错误标志条件,那么我将使用1)设置错误标志来捕获错误,然后如果条件成功,则清除错误标志。这样就没有错过错误的可能性。
For everything else I'd use 3)
对于我使用的其他一切3)
The NOT logic just adds to confusion when reading the code - well in my head, can't speak for eveyone else :-)
NOT逻辑只会在阅读代码时增加混乱 - 在我脑海中,不能代表eveyone :-)
#11
0
If the variable has a natural default value I would go with #1. If either value is equally (in)appropriate for a default then I would go with #2.
如果变量具有自然默认值,我将使用#1。如果任何一个值等于(默认)适合默认值,那么我将使用#2。
#12
0
It depends. I like the ternary operators, but sometimes it's clearer if you use an 'if' statement. Which of the four alternatives you choose depends on the context, but I tend to go for whichever makes the code's function clearer, and that varies from situation to situation.
这取决于。我喜欢三元运算符,但有时候如果使用'if'语句则更清楚。您选择的四种替代方案中的哪一种取决于上下文,但我倾向于选择哪种方式使代码的功能更加清晰,并且因情况而异。