记住三元运算符语法

发布于 2024-08-05 06:30:03 字数 61 浏览 4 评论 0原文

有人有记住标准三元语法的好技巧吗?

具体是否是“?”或“:”在前。多年来我一直在倒退这一点。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

誰ツ都不明白 2024-08-12 06:30:03

您正在检查的条件有点像问题,因此问号排在第一位。

x > 0 ? 1 : 0

将此语句视为三个英语句子:“Is x 大于 0?Then 1。Else,0。”声明中的每个子句都有一个句子。

谓词:

x > 0 ? /* Is x greater than 0? */

“真”分支:

1 /* Then 1. */

“假”分支:

: 0 /* Else, 0. */

The condition you are checking is kind of like a question, so the question mark comes first.

x > 0 ? 1 : 0

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:

x > 0 ? /* Is x greater than 0? */

The "true" branch:

1 /* Then 1. */

The "false" branch:

: 0 /* Else, 0. */
走野 2024-08-12 06:30:03

至于记住哪个符号在前,我只想到第一部分是一个问题,“这是真的还是假的?”,所以问号在前。

我以这种方式思考语法

Question ? Yes : No

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

Question ? Yes : No
还在原地等你 2024-08-12 06:30:03

在Python中我把它当作一个普通的英语句子来读:

 a equals b if condition is true else c

in python I read it as a normal English sentence:

 a equals b if condition is true else c
夜司空 2024-08-12 06:30:03

可以这样想:三元语句由三部分组成:问题、问题答案为“是”时执行的代码以及答案为“否”时执行的代码。这 ”?”像英语句子一样出现在问题之后。

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.

三生路 2024-08-12 06:30:03

“?”是一个问号,所以它的意思是“如果”。

冒号的意思是“现在来了”,“然后就做”。

三元运算符的好处是您不必被迫使用它,特别是当您在记住语法时遇到问题时。只需使用大多数情况下更具可读性的 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.

帅气称霸 2024-08-12 06:30:03

事情是这样的:

myVariable = this.testMethod() ? 'value for true case' : 'value for false case'

It goes like this:

myVariable = this.testMethod() ? 'value for true case' : 'value for false case'
乙白 2024-08-12 06:30:03

三元运算符的语法为? <如果为真> :

? 表示 if

但是,如果将 ? 放在 if 等表达式前面,则语法将变为 ? <条件> <如果为真> :<如果为假>

<代码><条件>很难区分,所以必须添加其他符号来分割它,这违背了三元运算符简化语法的目的。

所以我们必须把 ? 放在它后面才能解决这个问题,所以它是一个倒置的设置,可以看成 (?); :<如果为假>

其内容为:如果,则,否则

The syntax of ternary operators is <condition> ? <if-true> : <if-false>

? means if.

But if you put ? in front of an expression like if, 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>.

意犹 2024-08-12 06:30:03

如果你的单元测试在出错时仍然通过,那么要么没关系,要么你的测试没有覆盖代码中足够的路径。如果输入内容和测试通过/失败之间的间隔太长,那就是另一个问题了。在良好、快速的测试中,几乎没有什么语法问题很重要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文