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?
I always thought that parentheses improved readability, but in my textbook there is a
Share
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, ifais anObject,!aby itself is an error, so clearly!a.isSomething()must negate the return value of the method call.