有哪些例子可以说明在程序中使用括号会降低可读性?
我一直认为括号提高了可读性,但在我的教科书中有这样一句话:使用括号会极大地降低程序的可读性。有人有例子吗?
I always thought that parentheses improved readability, but in my textbook there is a statement that the use of parentheses dramatically reduces the readability of a program. Does anyone have any examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我可以找到很多反例,其中缺少括号降低了可读性,但我能想到的作者可能的意思的唯一示例是这样的:
在上面的情况下, ( )围绕方法调用是不必要的,这种代码可能会受益于将项分解为变量。由于条件中间有所有这些近括号,因此很难准确地看出什么与什么分组。
我认为上面的内容更具可读性,但这只是个人意见。这可能就是作者所要表达的意思。
括号的一些很好的用途:
System.out.println("答案是 " + (a + b));
可能令人困惑的使用of parens:
a.isSomething()
前面。在 Java 中,如果a
是一个Object
,则!a
本身就是一个错误,所以显然!a.isSomething()< /code> 必须否定方法调用的返回值。
I can find plenty of counterexamples where the lack of parentheses lowered the readability, but the only example I can think of for what the author may have meant is something like this:
In the above case, the ( ) around the method calls is unnecessary, and this kind of code may benefit from factoring out of terms into variables. With all of those close parens in the middle of the condition, it's hard to see exactly what is grouped with what.
I think the above is more readable, but that's a personal opinion. That may be what the author was talking about.
Some good uses of parens:
System.out.println("The answer is " + (a + b));
Possibly confusing use of parens:
a.isSomething()
above. In Java, ifa
is anObject
,!a
by itself is an error, so clearly!a.isSomething()
must negate the return value of the method call.显然,你的教科书是由讨厌 Lisp 的人写的。
无论如何,这只是品味问题,没有适合每个人的唯一真理。
Apparently, your textbook is written by someone who hate Lisp.
Any way, it's a matter of taste, there is no single truth for everyone.
好吧,考虑这样的事情:
Result = (x * y + p * q - 1) % t 和
Result = (((x * y) + (p * q)) - 1) % t
就我个人而言,我更喜欢前者(但那只是我),因为后者让我认为括号是为了改变实际的操作顺序,而事实上他们并没有这样做。您的教科书可能还会提到何时可以将计算拆分为多个变量。例如,在求解二次
ax^2+bx+c=0
时,您可能会遇到类似的情况:x1 = (-b + sqrt(b*b - 4*a *c)) / (2*a)
这看起来确实有点难看。在我看来,这看起来更好:
这只是一个简单的例子,当您使用涉及大量计算的算法时,事情可能会变得非常丑陋,因此将计算分成多个部分将比括号更有利于可读性。
Well, consider something like this:
Result = (x * y + p * q - 1) % t
andResult = (((x * y) + (p * q)) - 1) % t
Personally I prefer the former (but that's just me), because the latter makes me think the parantheses are there to change the actual order of operations, when in fact they aren't doing that. Your textbook might also refer to when you can split your calculations in multiple variables. For example, you'll probably have something like this when solving a quadratic
ax^2+bx+c=0
:x1 = (-b + sqrt(b*b - 4*a*c)) / (2*a)
Which does look kind of ugly. This looks better in my opinion:
And this is just one simple example, when you work with algorithms that involve a lot of computations, things can get really ugly, so splitting the computations up into multiple parts will help readability more than parantheses will.
我认为括号并不是提高代码可读性的最佳方法。您可以使用换行符来下划线,例如 if 语句中的条件。如果不需要,我不会使用括号。
I think that parentheses is not a best way to improve readability of your code. You can use new line to underline for example conditions in if statement. I don't use parentheses if it is not required.
当括号明显多余时,会降低可读性。读者期望他们在那里是有原因的,但实际上没有理由。因此,出现了认知打嗝。
“明显”多余是什么意思?
当可以删除括号而不改变程序的含义时,括号是多余的。
用于消除中缀运算符歧义的括号不是“明显冗余”,即使它们是冗余的,除非在乘法和加法运算符的非常特殊的情况下。原因:许多语言都有 10 到 15 级优先级,许多人使用多种语言工作,没有人能记住所有规则。即使括号是多余的,最好还是消除歧义。
所有其他冗余括号显然是多余的。
在学习新语言的人编写的代码中经常会发现多余的括号;也许新语法的不确定性会导致防御性括号。
消灭他们!
您要求提供示例。以下是我在初学者编写的 ML 代码和 Haskell 代码中反复看到的三个示例:
if (...) then
之间的括号总是多余且分散注意力。它们使作者看起来像一个 C 程序员。只需编写if ... then
。变量周围的括号很愚蠢,如
print(x)
中那样。变量周围不需要括号;函数应用程序应该写成print x
。如果函数应用程序是中缀表达式中的操作数,则该函数应用程序周围的括号是多余的。例如,
<前><代码>(长度xs)+ 1
应该总是写
Parentheses reduce readability when they are obviously redundant. The reader expects them to be there for a reason, but there is no reason. Hence, a cognitive hiccough.
What do I mean by "obviously" redundant?
Parentheses are redundant when they can be removed without changing the meaning of the program.
Parentheses that are used to disambiguate infix operators are not "obviously redundant", even when they are redundant, except perhaps in the very special case of multiplication and addition operators. Reason: many languages have between 10–15 levels of precedence, many people work in multiple languages, and nobody can be expected to remember all the rules. It is often better to disambiguate, even if parentheses are redundant.
All other redundant parentheses are obviously redundant.
Redundant parentheses are often found in code written by someone who is learning a new language; perhaps uncertainty about the new syntax leads to defensive parenthesizing.
Expunge them!
You asked for examples. Here are three examples I see repeatedly in ML code and Haskell code written by beginners:
Parentheses between
if (...) then
are always redundant and distracting. They make the author look like a C programmer. Just writeif ... then
.Parentheses around a variable are silly, as in
print(x)
. Parentheses are never necessary around a variable; the function application should be writtenprint x
.Parentheses around a function application are redundant if that application is an operand in an infix expression. For example,
should always be written
任何极端和/或过度使用的行为都会使代码变得不可读。通过评论提出同样的主张并不难。如果您曾经看过几乎每一行代码都有注释的代码,您会发现它很难阅读。或者,您可以在每行代码周围有空格,这将使每行易于阅读,但通常大多数人希望将类似的相关行(不保证突破方法)分组在一起。
Anything taken to an extreme and/or overused can make code unreadable. It wouldn't be to hard to make the same claim with comments. If you have ever looked at code that had a comment for virtually every line of code would tell you that it was difficult to read. Or you could have whitespace around every line of code which would make each line easy to read but normally most people want similar related lines (that don't warrant a breakout method) to be grouped together.
你必须对它们进行过多的处理才能真正损害可读性,但作为个人品味的问题,我总是发现;
在 C 和 C++ 代码中类似的情况非常令人恼火。
You have to go way over the top with them to really damage readability, but as a matter of personal taste, I have always found;
and similar in C and C++ code to be very irritating.
如果方法不带参数,为什么需要空的
()
来调用method()
?我相信 groovy 你不需要这样做。If a method doesn't take parameters why require an empty
()
to callmethod()
? I believe in groovy you don't need to do this.