Tips & Tricks

Parentheses Inspections & Intentions

You know the “Unnecessary Parentheses” inspection? IntelliJ IDEA has had it for quite some time. And if you were a user of the InspectionGadgets plugin since before it was bundled with IntelliJ IDEA, you know it even longer. The inspection reports any parentheses which do not alter the default evaluation order of an expression. For example:

It’s a nice inspection, but it may be a little greedy. I write code not only for the compiler, but also to be read by me and other people. Sometimes the parentheses highlighted by the inspection do have a use other than modifying the order of evaluation: they serve to clarify the expression.
Often while reading some new or old code, I encounter complex expressions which are quite hard to understand, especially so when they don’t have any parentheses to clarify the evaluation order. Take for example the following (still quite simple) code:

The new “Add Clarifying Parentheses” intention can help understand complex expressions, by adding a few well placed parentheses. This intention is available in Demetra since build #5218. While strictly unnecessary, with parentheses understanding the expression takes a little less brainpower.

I think there should be an option in the “Unnecessary Parentheses” inspection to ignore clarifying parentheses, don’t you agree?

Technorati tags: , , ,

Discover more