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?

Comments below can no longer be edited.

6 Responses to Parentheses Inspections & Intentions

  1. peter royal says:

    June 12, 2006

    i agree. ‘return (42)’ is completely worthless, but your last example is useful.

  2. etienne says:

    June 12, 2006

    Very nice. The option to ignore clarifying parenthesis would be great. Though, the question is, which parentheses are clarifying and which ones are only redundant 😉

  3. Ahmed Mohombe says:

    June 13, 2006

    Very good point Etiene :).
    I don’t think such a rule exists, nor do I think that one could define/implement it. IMHO the level of “clarification” is very subjective dependent on ones math knowledge/working experience(e.g. for longer expressions only a few parentheses are usefull).

  4. BasLeijdekkers says:

    June 14, 2006

    Indeed Etienne and Ahmed, the question of when parentheses are clarifying is open for debate. I had meant to elaborate on how the intention determines clarification, probably the most important information there is about the intention, but it slipped my mind:-) Here is how the intention does it:

    Parentheses are clarifying if they surround a binary expression, that expression is itself contained in a parent binary expression and the operator of the parent and child expression is not equal. There is one exception, parentheses surrounding an instanceof expression contained in a parent binary expression are also considered clarifying by the intention.

    Any improvements on this heuristic are welcome.

  5. etienne says:

    June 16, 2006

    That sounds good! Thanks for the clarification.

    One more case I could imagine is about negation:
    boolean b=…;
    boolean c=…;
    boolean result = !b||c

  6. BasLeijdekkers says:

    June 17, 2006

    Unary operators, like the not operator, are always evaluated before binary operators, on the same operands. This becomes quite obvious if you add some whitespace:
    boolean result = !b || c;
    If the intention surrounded single operands with parentheses every time in cases like these, it would get old pretty soon:
    boolean result = !(b) || c;
    I might have added one to let it parenthesize the operands of unary operators too, if it where possible for intentions to have options. But it isn’t, so that makes this an example that highlights the importance of whitespace.


Subscribe for updates