应该彻底了解运算符优先级吗?

发布于 2024-07-12 02:04:26 字数 89 浏览 6 评论 0原文

程序员是否应该彻底了解运算符优先级? 使用大括号对表达式进行分组应该没问题,不是吗? 为了安全起见,我总是使用牙套。 当被问及优先级问题时,我无法轻易回答。

Should the programmer be aware of operator precedence thoroughly?
Using braces to group expressions should be okay, isn't? I always uses braces to be on safer side. And when asked a question on precedence, I cannot answer readily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(18

舞袖。长 2024-07-19 02:04:26

对于非常常见的操作员 - 是的,值得了解它。 如果您将依赖于运算符优先级的所有内容都放在括号内,那么您的代码将不可读(或容易与 Lisp 混淆)。

对于更晦涩难懂的运算符 - 我明确避免学习它们。 如果我开始认为太多事情是理所当然的,而我无法合理地期望其他开发人员知道,我就会开始编写我可以轻松阅读但其他人无法阅读的代码。 最好的例子是移位运算符。 我什至不知道(当然 - 我有一个暗示)如果没有括号,其中哪一个会被同等对待,但我绝对确定哪个更清楚:

int x = (y << shift) + offset;

int x = y << (shift + offset);

For very common operators - yes, it's worth knowing it. If you put brackets round everything that would otherwise rely on operator precedence, your code would be unreadable (or easily confused with Lisp).

For more obscure operators - I've explicitly avoided learning them. If I start taking too many things for granted that I can't reasonably expect other developers to know, I'll start writing code which I can read easily but no-one else can. The best example is shift operators. I don't even know (for sure - I have an inkling) which of these would be treated the same without the brackets, but I'm absolutely sure which is clearer:

int x = (y << shift) + offset;

or

int x = y << (shift + offset);
梦里梦着梦中梦 2024-07-19 02:04:26

是的,了解运算符优先级很重要。 然而,我认为使用括号来完全清楚地表达你的意图是有价值的。 它将有助于可读性和可维护性。

Yes, it's important to understand operator precedence. However, I think there's value in using parentheses to be perfectly clear about your intention. It will help readability and maintainability.

抚你发端 2024-07-19 02:04:26

几年前,我看到了这个 C 语言的优先级表:

  • 乘法和除法发生在加法和减法之前。
  • 使用括号。

这有点过于简化,但它的想法是正确的。

Years ago I saw this precedence table for C:

  • Multiplication and division occur before addition and subtraction.
  • Use parentheses.

That's slightly over simplified, but it has the right idea.

谁对谁错谁最难过 2024-07-19 02:04:26

除了最常见的算术运算符之外,我都使用备忘单。 如果事情有一点棘手,我会使用明确的括号来明确我的意图。

我喜欢使用书面指南而不是记住优先级的原因是我经常使用多种语言进行工作,并且规则略有不同。 当我开始过于依赖记忆时,记住最常见运算符之外的任何内容都可能会导致错误。

I use a cheat sheet for all but the most common arithmetic operators. If something is the least bit tricky, I use explicit parentheses to make my intentions clear.

The reason I like using a written guide rather than memorizing precedence is that I often work in multiple languages with subtly different rules. Memorizing anything beyond the most common operators can lead to errors when I begin to rely too much on my memory.

很糊涂小朋友 2024-07-19 02:04:26

你应该理解它,但你不应该假设别人会理解。 我总是使用括号来明确优先级。

You should understand it, but you should not assume that others will. I always use parenthesis to make the precedence explicit.

小镇女孩 2024-07-19 02:04:26

我知道基础知识(例如,除法和乘法比加法和减法更高),但我必须查找更深奥的东西。

我通常使用括号来表达我的意图。 我认为它读起来更好,并且避免了由于不正确的假设而导致的错误。

I know the basics (e.g., division and multiplication higher than addition and subtraction), but I would have to look up something more esoteric.

I usually use parentheses to make my intention clear. I think it reads better, and avoids errors due to incorrect assumptions.

追风人 2024-07-19 02:04:26

它当然不会有什么坏处,并且可以帮助您阅读其他人编写的具有优先微妙之处的代码。

但在实践中,最好不要假设维护代码的人员具有相同的技能,因此虽然应该能够阅读此代码,但您可能不希望编写过多利用交互的代码。 最好使用明确无误的结构,即使这意味着到处都有一些额外的括号。

It certainly couldn't hurt, and can help you read code with precendence subtleties written by others.

In practice, though, it's probably better not to assume that those maintaining your code will have the same skill, so while should be able to read this code you may not want to write code that takes too much advantage of the interactions. Better to use constructs that are unmistakable, even if it means a few extra parentheses here and there.

梦中楼上月下 2024-07-19 02:04:26

在这一点上我同意你的看法; 使用大括号来明确优先级总是一个好主意。 它使您的代码更易于阅读,并且您的意图很明确。

话虽如此,如果您正在维护其他人的代码,您应该知道在哪里查找有关运算符优先级的信息。

I'm with you on this one; using braces to make the precedence explicit is always a good idea. It makes your code easier to read, and your intentions are explicit.

With that said, you should know where to look up information on operator precedence, in case you are maintaining others' code.

七秒鱼° 2024-07-19 02:04:26

实际上:并非如此。 如果它太复杂以至于您必须知道运算符优先级,那么无论如何您都应该将表达式分解为块。 另外,括号会拯救你!

专业:学习您选择的语言的顺序并不需要很长时间,并且在阅读其他人的代码时可以派上用场。 如果没有别的事,就把它放在你的立方体墙上的备忘单上。

Practically: Not really. If it's so complicated that you have to know operator precedence, you should probably break up the expression into chunks anyway. Also, parenthesis will save you!

Professionally: It doesn't take long to learn the order for your language of choice and can come in handy when reading other people's code. If nothing else, just keep it on a cheat-sheet on your cube wall.

梦罢 2024-07-19 02:04:26

您应该理解它,以便能够阅读那些没有在任何地方使用括号的人编写的代码。

我不认为“总是大括号”是一个好的经验法则。 具有太多括号的代码会变得比必要的更难阅读。 喜欢:

(a * b) + c

只是愚蠢的。 某个地方存在着平衡,挑战在于找到它。 就像许多其他事情一样,它需要运用常识。

You should understand it to be able to read the code written by someone who hasn't used parens everywhere.

I don't think "always-braces" is a good rule of thumb. Code that has too many parens gets harder to read than necessary. Like:

(a * b) + c

is just silly. There's a balance somewhere and the challenge is to find it. Like so many other things it's about applying common sense.

妄断弥空 2024-07-19 02:04:26

Quick,在 T-SQL 中具有更高的优先级:AND 与 OR。

SELECT c.Name
FROM [Customer] c LEFT JOIN [Order] o
  ON c.CustomerKey = o.CustomerId
WHERE c.Name like 'B%'
  OR c.Name like 'Z%'
  AND o.CustomerId is NULL

一旦您了解这些运算符的优先级到您依赖该优先级的程度,您就注定会:

  • 自己错误地使用它们。
  • 遭受其他人的错误,他们不会学习这些运算符,也无法阅读您的代码。

Quick, which has higher precedence in T-SQL: AND versus OR.

SELECT c.Name
FROM [Customer] c LEFT JOIN [Order] o
  ON c.CustomerKey = o.CustomerId
WHERE c.Name like 'B%'
  OR c.Name like 'Z%'
  AND o.CustomerId is NULL

Once you learn the precedence of these operators to the point you rely on that precedence, you're doomed either to:

  • Use them wrong yourself.
  • Suffer the mistakes of others, who will not learn these operators and cannot read your code.
段念尘 2024-07-19 02:04:26

我认为了解基本的优先规则很重要。 我发现了解具体细节并不值得花时间。 主要是因为任何优先级不明确的情况都可以用一组简单的括号来解决。 其次,因为并不是每个人都知道它们,所以最好让您的代码对组中的用户可读。

在大学的时候,我曾经熟记所有的优先规则。 当我进入现实世界后,我发现大多数人都没有记住它们。 如果我签入一些依赖于某些优先规则的有效代码,我始终会得到相同的反馈“添加括号以使其明显”。

I think it's important to understand the basic predidence rules. I find knowing the nitty gritty details to be not worth the time. Mainly because any case where predecence is ambiguous can be solved with a simple set of parens. Secondly because not everyone knows them and it's best to make your code readable to the users in your group.

At one time in college, i used to know all of the predence rules by heart. Once I got into the real world I found most people did not know them by heart. If i checked in some valid code which depended upon some predecence rules I consistently got the same feedback "add parens to make it obvious".

七分※倦醒 2024-07-19 02:04:26

还有第三种选择。 与其记住所有这些规则,使用括号来确保一切......

如果您的表达式太长以至于不清楚首先发生什么,请将其分成多行,使用临时变量中间结果。 然后,您将引入清晰的概念,而无需使用混乱的括号,并且您可以使用临时变量的描述性名称来使事情更加清晰。

There's a third option. Rather than memorizing all those rules, or using parentheses to make sure of everything...

If your expression is so long that it's not clear what happens first, break it up into multiple lines, using temp variables for intermediate results. Then you're introducing clarity without the clutter of parentheses, and you can use descriptive names for the temp variables to make things even more clear.

自我难过 2024-07-19 02:04:26

不!

如果您不确定优先级,那么其他阅读代码的人很可能会错误地解释它。

如果您不确定

 ( .. ) 

the relevent bits of code then there is no ambiguity for you or anyone else.

No!

If you are unsure of the precedence then its probable that anyone else reading the code would be liable to interpret it wrongly.

If you are unsure just

 ( .. ) 

the relevent bits of code then there is no ambiguity for you or anyone else.

九歌凝 2024-07-19 02:04:26

我从来没有 100% 掌握优先级规则,因为我使用多种语言(C、C++、perl、bash,偶尔也使用一些 TCL/TK)。 多年来,我在不同时期使用过几种不同的汇编语言和一两个不合格的编译器。

我无法将完整详细的优先规则直接记在脑子里——我必须经常在规则集之间切换。

I never have a 100% grip on the precedence rules because I work in several languages (C, C++, perl, bash, and once in a while a dash of TCL/TK.). At various times over the years I've worked with several different assembly languages and a non-conforming compiler or two.

I CAN'T keep the fully detailed precedence rules straight in my head - I have to switch among rule sets too often.

橪书 2024-07-19 02:04:26

是 对于:

  • * / + -
  • && || 和或

否/也许:

  • << >>> & | ^

Yes For:

  • * / + -
  • && || And Or

No/Maybe:

  • << >> & | ^
鹤舞 2024-07-19 02:04:26

当编写 a == a & 时,很容易欺骗自己。 一些_标志

即使我建议不要学习优先级规则,为了清楚起见,使用括号和临时变量(记住:代码是要由人类阅读的),有时会出现一个运算符优先级表派上用场。

It is very very easy to fool yourself when writing a == a & some_flag.

Even if I would suggest not learning precedence rules, using parentheses and temporaries for clarity (remember : code is meant to be read by human beings), sometimes an operator precedence table comes in handy.

你的心境我的脸 2024-07-19 02:04:26

假设您了解运算符优先级的概念(我假设每个人都从数学课上知道),我宁愿不假设读者有任何知识......

除了常识:正如已经指出的那样,像 a * b + c 这样的表达式可以使用数学课上的知识来理解。 布尔表达式,例如 a < 4、&& a> 0 可以理解,因为比较绑定比 && 更符合逻辑。

另一方面,将按位运算符与普通算术混合使用并不存在常识,因此最好在此处使用括号。 && 优先于 || 也没有常识(或者是相反?),所以也在那里使用括号。

Assuming you know about the concept of operator precedence (which I assume everyone does, from math classes), I'd rather not assume any knowledge on the reader's side...

...except for common sense: As has been pointed out, expressions like a * b + c can be understood using knowledge from math classes. Boolean expressions like a < 4 && a > 0 can be understood because it's more logical to have comparisons bind tighter than &&.

On the other hand, there exists no common sense for mixing bitwise operators with normal arithmetic, so better use parentheses there. There is also no common sense for precedence of && over || (or was it the other way around?), so also use parentheses there.

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