记住三元运算符语法
有人有记住标准三元语法的好技巧吗?
具体是否是“?”或“:”在前。多年来我一直在倒退这一点。
Anyone have a good trick to remember the standard ternary syntax?
Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您正在检查的条件有点像问题,因此问号排在第一位。
将此语句视为三个英语句子:“Is x 大于 0?Then 1。Else,0。”声明中的每个子句都有一个句子。
谓词:
“真”分支:
“假”分支:
The condition you are checking is kind of like a question, so the question mark comes first.
Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement.
The predicate:
The "true" branch:
The "false" branch:
至于记住哪个符号在前,我只想到第一部分是一个问题,“这是真的还是假的?”,所以问号在前。
我以这种方式思考语法
As far as remembering which symbol comes first, I just think of the fact that the first part is a question, "Is it true or not?", so the question mark goes first.
I think of the syntax in this manner
在Python中我把它当作一个普通的英语句子来读:
in python I read it as a normal English sentence:
可以这样想:三元语句由三部分组成:问题、问题答案为“是”时执行的代码以及答案为“否”时执行的代码。这 ”?”像英语句子一样出现在问题之后。
Think of it this way: a ternary statement consists of three parts: the question, the code to execute if the answer to the question is "yes" and the code if the answer is "no". The "?" comes after the question like it does in English sentences.
“?”是一个问号,所以它的意思是“如果”。
冒号的意思是“现在来了”,“然后就做”。
三元运算符的好处是您不必被迫使用它,特别是当您在记住语法时遇到问题时。只需使用大多数情况下更具可读性的 if 语句即可。
不,三元没有比 if 语句更好的性能。
"?" is a question mark so it means "if".
A colon means, "now it comes", "then do".
The good thing about the ternary operator is that you are not forced to use it, especially if you are having problems remembering the syntax. Just use an if-statement which is more readable most times.
And no - the ternary has no better performace then an if-statement.
事情是这样的:
It goes like this:
三元运算符的语法为? <如果为真> :
?
表示if
。但是,如果将
?
放在if
等表达式前面,则语法将变为? <条件> <如果为真> :<如果为假>
。<代码><条件>很难区分,所以必须添加其他符号来分割它,这违背了三元运算符简化语法的目的。
所以我们必须把
?
放在它后面才能解决这个问题,所以它是一个倒置的设置,可以看成(?); :<如果为假>
。其内容为:如果
,则
,否则
。The syntax of ternary operators is
<condition> ? <if-true> : <if-false>
?
meansif
.But if you put
?
in front of an expression likeif
, the syntax becomes? <condition> <if-true> : <if-false>
.<condition> <if-true>
is hard to distinguish, so you have to add other symbols to split it, which defeats the purpose of the ternary operator to simplify the syntax.So we have to put
?
comes after it to solve this problem, so it is an inverted setting, which can be seen as(<condition> ?) <if-true> : <if-false>
.which reads: If
<condition>
then<if-true>
, otherwise<if-false>
.如果你的单元测试在出错时仍然通过,那么要么没关系,要么你的测试没有覆盖代码中足够的路径。如果输入内容和测试通过/失败之间的间隔太长,那就是另一个问题了。在良好、快速的测试中,几乎没有什么语法问题很重要。
If you're unit tests still pass when you get it wrong, then either it doesn't matter or your tests aren't covering enough of the paths through the code. If there's too long a gap between typing it and getting a pass/fail from the tests, that's another issue. Very few little syntax nits matter in the presence of good, fast tests.