net.sourceforge.pmd.rules.design

Class ConfusingTernary

public class ConfusingTernary extends AbstractRule

if (x != y) { diff(); } else { same(); } and
(!x ? diff() : same());.

XPath can handle the easy cases, e.g.:

    //IfStatement[
      Statement[2]
      and Expression[
        EqualityExpression[@Image="!="] or
        UnaryExpressionNotPlusMinus[@Image="!"]]]
 
but "&&" and "||" are difficult, since we need a match for all children instead of just one. This can be done by using a double-negative, e.g.:
    not(*[not(matchme)])
 
Still, XPath is unable to handle arbitrarily nested cases, since it lacks recursion, e.g.:
   if (((x != !y)) || !(x)) { diff(); } else { same(); }
 
Method Summary
Objectvisit(ASTIfStatement node, Object data)
Objectvisit(ASTConditionalExpression node, Object data)

Method Detail

visit

public Object visit(ASTIfStatement node, Object data)

visit

public Object visit(ASTConditionalExpression node, Object data)