What are some examples of where using parentheses in a program reduces readability?

I always thought that the readability in brackets was improved, but in my textbook there is a statement that using parentheses significantly reduces the readability of the program. Does anyone have any examples?

+4
source share
8 answers

I can find many counterexamples where the lack of parentheses has reduced readability, but the only example I can think of could mean the following:

if(((a == null) || (!(a.isSomething()))) && ((b == null) || (!(b.isSomething())))) { // do some stuff } 

In the above case, calling () around method calls is not needed, and this kind of code can benefit from factoring from terms to variables. With all these close parens in the middle of the condition, it's hard to understand what is grouped with what.

 boolean aIsNotSomething = (a == null) || !a.isSomething(); // parens for readability boolean bIsNotSomething = (b == null) || !b.isSomething(); // ditto if(aIsNotSomething && bIsNotSomething) { // do some stuff } 

I think the above is more readable, but this is a personal opinion. This may be what the author was talking about.

Some good uses of partners:

  • to distinguish the order of work when changing behavior without parens parameters
  • in order to differentiate the order of work when the behavior is not affected, but someone who does not know the binding rules well enough will read your code. The rule of a good citizen.
  • to indicate that an expression within parens should be evaluated before using the larger expression: System.out.println("The answer is " + (a + b));

Perhaps confusing use of partners:

  • in places where it cannot have any other meaning, for example, before a.isSomething() above. In Java, if a is Object !a.isSomething() is an error in itself, so clearly !a.isSomething() should negate the return value of the method call.
  • to link together a large number of conditions or expressions that would be clearer if they were broken. As in the above code example, splitting the large paratetic operator into smaller pieces allows a simpler implementation of the code in the debugger, and if conditions / values โ€‹โ€‹are needed later in the code, you do not end up repeating the expressions and doing the work twice. This is subjective, but obviously pointless if you only use expressions in one place and your debugger still shows you intermediate evaluated expressions.
+6
source

Apparently your tutorial is written by someone who hates Lisp.

In any case, it is a matter of taste, there is no single truth for everyone.

+5
source

I think parentheses are not the best way to improve the readability of your code. You can use a new line to emphasize, for example, conditions in an if statement. I do not use parentheses unless this is required.

+2
source

Ok, consider something like this:

Result = (x * y + p * q - 1) % t and

Result = (((x * y) + (p * q)) - 1) % t

Personally, I prefer the former (but it's just me), because the latter makes me think that the brackets are there to change the actual order of operations, when in fact they do not. Your tutorial may also mention when you can split your calculations into several variables. For example, you will probably have something like this when solving the quadratic ax^2+bx+c=0 :

x1 = (-b + sqrt(b*b - 4*a*c)) / (2*a)

Which looks kind of ugly. It looks better in my opinion:

 SqrtDelta = sqrt(b*b - 4*a*c); x1 = (-b + SqrtDelta) / (2*a); 

And this is just one simple example: when you work with algorithms that involve a lot of calculations, everything can become really ugly, so splitting the calculations into several parts will help readability more than brackets.

+2
source

Brackets reduce readability when they are clearly redundant . The reader expects them to be there for some reason, but no reason. Consequently, cognitive hiccups.

What do I mean by โ€œclearlyโ€ redundant?

  • Parentheses are redundant when they can be removed without changing the program value.

  • The brackets that are used to disambiguate the infix operators are not "explicitly redundant", even if they are redundant, except, perhaps, in the special case of the multiplication and addition operators. Reason: many languages โ€‹โ€‹have between 10 and 15 priority levels, many people work in several languages, and no one can expect them to remember all the rules. It is often better to eliminate ambiguity, even if the brackets are redundant.

  • All other redundant parentheses are clearly redundant.

Back-up parentheses are often found in code written by someone who is learning a new language; perhaps the ambiguity regarding the new syntax leads to a security bracket. Change them!


You asked for examples. Here are three examples that I see repeatedly in ML code and Haskell newbie codec:

  • The brackets between if (...) then always redundant and distracting. They make the author look like a C programmer. Just write if ... then .

  • The brackets around the variable are dumb, as in print(x) . Parentheses are never needed around a variable; function application must be written print x .

  • The braces around the functional application are redundant if this application is an operand in the infix expression. For instance,

     (length xs) + 1 

    should always be recorded

     length xs + 1 
+2
source

Everything that is customary to do extreme and / or abused can make the code unreadable. It would not be easy to make the same requirement with the comments. If you ever looked at code that had a comment for almost every line of code, you would say that it is difficult to read. Or you may have a space around each line of code that makes it easy to read each line, but usually most people want to group along with related lines (which do not require a breakout method).

+1
source

You have to go above to really damage readability, but as a matter of personal taste, I always found:

 return (x + 1); 

and similar in code C and C ++ are very annoying.

+1
source

If the method does not accept parameters, what does empty () for calling method() ? I believe in groovy that you do not need to do this.

0
source

Source: https://habr.com/ru/post/1304061/


All Articles