New Check: ConfusingConditionCheck #90

Closed
romani opened this Issue Nov 28, 2012 · 1 comment

Projects

None yet

3 participants

@romani
Member
romani commented Nov 28, 2012

from PMD http://pmd.sourceforge.net/pmd-5.0.0/rules/index.html:
ConfusingTernary: Avoid negation within an "if" expression with an "else" clause. For example, rephrase: if (x != y) diff(); else same();as: if (x == y) same(); else diff();Most "if (x != y)" cases without an "else" are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, suchas "does the error case go first?" or "does the common case go first?".

we just give idea from PMD, we have the constant problem in code - so we need to automate it a bit at least as in PMD.
We need to check even short form of conditions "?(a != b)c=1:c=2;"

@romani
Member
romani commented Dec 25, 2012

Lets split task in two stages: simple Check for "!=" detection and advanced check to detect more complicated conditions. I am not 100% sure in success of second stage so lets finish+test first part and then try to do second in separate branch.

In case it is ternary(http://en.wikipedia.org/wiki/%3F:), we could require modification (see "discrete math" for details, http://en.wikipedia.org/wiki/De_Morgan%27s_laws):
!a ====> a
!a && !b ====> a && b
a != b ====> a ==b
a != b && c != d ====> a ==b && c ==d
!a || !b ====> a && b

@VadimPanasiuk VadimPanasiuk added a commit to VadimPanasiuk/sevntu.checkstyle that referenced this issue Aug 28, 2013
@VadimPanasiuk VadimPanasiuk Issue #90: Confusing Termary. fixed 3045e29
@VadimPanasiuk VadimPanasiuk added a commit to VadimPanasiuk/sevntu.checkstyle that referenced this issue Aug 28, 2013
@VadimPanasiuk VadimPanasiuk Issue #90: Confusing Termary. fixed 74a8089
@VadimPanasiuk VadimPanasiuk added a commit to VadimPanasiuk/sevntu.checkstyle that referenced this issue Aug 28, 2013
@VadimPanasiuk VadimPanasiuk Issue #90: Confusing Termary. fixed e90acfa
@VadimPanasiuk VadimPanasiuk added a commit to VadimPanasiuk/sevntu.checkstyle that referenced this issue Sep 2, 2013
@VadimPanasiuk VadimPanasiuk Issue #90: Confusing Termary. fixed2 dbec575
@isopov isopov closed this Sep 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment