开发者

A "if" condition which result is false but my program is going trough this if anyway ??? (true or false)

开发者 https://www.devze.com 2023-01-29 13:07 出处:网络
In my JAVA program, i have this \"if\" condition : if((!pccoNettNoAff && !transOPCVM && !garantie)

In my JAVA program, i have this "if" condition :

    if(  (!pccoNettNoAff && !transOPCVM && !garantie) 
      && (  (pccoCourant == null) 
         || (  (pccoCourant != null && rBale.getPcco() != null) 
            && (  (pccoCourant.getId() != rBale.getPcco().getId()) 
               || (  pccoCourant.getId() == rBale.getPcco().getId() 
                  && tauxCourant!=null && rBale.getTauxCcf()!开发者_运维百科=null 
                  && rBale.getPartenaire()==null 
                  && rBale.getTauxCcf()!=tauxCourant
                  )
               )      
            )
         )
      )
   {

We can translate this in logic by :

Legend : T=true and F=False : They are the result of each test

((T) AND ((F) OR ((T) AND ((F) OR (F)))))

So my final result is obviously False (the Eclipse debug mode find the same result) But my program is going trough this "if" anyway.

I have really not ideas why this is happening, maybe there is some sort of limitation in a "if" condition ??

If anyone have any ideas, please help me :)

Regards,

Cytemax


I know why it's happening - because that is far too complicated for an in-line condition.

Refactor that code into a method (or possibly several), whereby you can put things on multiple lines, add temporary variables etc., and it will become much clearer both what it is meant to do, and why it isn't doing it. Something like the following:

private boolean shouldBeActedOn(PCCO pccoCourant) {
   if (pccoCourant != null) {
      return true;
   }
   final PCCO balePcco = rBale.getPcco()
   if (balePcco != null) {
       // etc.
   }
   ...
}

// Then later, where your current block is:

  if (shouldBeActedOn(pccoCourant, otherArgs)) {
     ...
  }

Rest assured, Java's if statements work correctly.


One idea: don't have such a ridiculous number of conditions in a single statement.

You've got virtually no chance of working out what's going on here without simplifying it. Extract subexpressions into separate local variables, and then make the if condition just combine those local variables.

That way you've got a much better chance of working out what's going on. Chances are you've misinterpreted the debugger somewhere...


If .getId() returns a String, the == and != may not work the way you think - it will test whether they are the same instances, not whether they are equal strings. Look into String.equals().


Not a direct answer to your question, but I'll echo what others have said. First and foremost, simplify your conditional logic; perhaps reduce via boolean algreba rules (if the branching is too complex).

Reminds of me karnaugh maps in school.

I like to define local variables for each separate, smaller condition so that I can combine them into essentially a single If statement. Each boolean value can call out to a separate function for improved legibility.

boolean isTrainingDue( int numDaysLeft ) {

  boolean trainingDue = false;
  boolean isNewUser = (userService.daysSinceSignUp() < 30);
  boolean gracePeriodExpired = (userService.daysLeft() < 1);
  boolean notCertified = 
        !ObjectUtils.isEmpty(p.getExpiredDate()) && 
         ObjectUtils.isEmpty(p.getCertifiedDate());  

  if (isNewUser && notCertified && gracePeriodExpired)
    trainingDue = (numDaysLeft <= 30);

  return trainingDue;       
}


UPDATED

OK, I was flat out wrong on this one--missed some -parentheses...

Anyhow, I think Bert F is onto something...

You're checking to see if things (pccoCourant and rBale.getTauxCcf()) are not null, implying they are object references.

You then check to see if they are the same (using ==).

You probably want to check to see if they are EQUAL:

 if( 
(!pccoNettNoAff && !transOPCVM && !garantie) 
&& ( 
    (pccoCourant == null) 
        || ( 
            (pccoCourant != null && rBale.getPcco() != null) 
            && ( 
                (pccoCourant.getId() != rBale.getPcco().getId()) 
                || (
                    pccoCourant.getId() == rBale.getPcco().getId() 
                    && tauxCourant!=null && rBale.getTauxCcf()!=null 
                    && rBale.getPartenaire()==null 
                    && ! rBale.getTauxCcf().equals(tauxCourant)
                )
            )      
    )
)
 ){

You can ignore the rest of this:

Aside from the complexity mentioned by the others, you have some ambiguity in your later conditions:

 if( 
(!pccoNettNoAff && !transOPCVM && !garantie) 
&& ( 
    (pccoCourant == null) 
        || ( 
            (pccoCourant != null && rBale.getPcco() != null) 
            && ( 
                (pccoCourant.getId() != rBale.getPcco().getId()) 
                || (pccoCourant.getId() == rBale.getPcco().getId() 
                && tauxCourant!=null && rBale.getTauxCcf()!=null 
                && rBale.getPartenaire()==null 
                && rBale.getTauxCcf()!=tauxCourant)
            )      
    )
    )
 ){

The innermost nested conditions are lumping an OR with the ANDS at the same level. I suspect what you want is:

 if( 
(!pccoNettNoAff && !transOPCVM && !garantie) 
&& ( 
    (pccoCourant == null) 
        || ( 
            (pccoCourant != null && rBale.getPcco() != null) 
            && ( 
                (pccoCourant.getId() != rBale.getPcco().getId()) 
                || (
                    (pccoCourant.getId() == rBale.getPcco().getId() 
                    && tauxCourant!=null && rBale.getTauxCcf()!=null 
                    && rBale.getPartenaire()==null 
                    && rBale.getTauxCcf()!=tauxCourant)
                )
            )      
    )
)
 ){


I agree with the posts by others, but aside from that, you just didn't structure the logic in an easily read way. Here is my attempt and making sense of this long predicate condition:

if(
    !pccoNettNoAff
    && !transOPCVM
    && !garantie
    && (
        pccoCourant == null
        ||
        pccoCourant != null
        && rBale.getPcco() != null
        && (
            pccoCourant.getId() != rBale.getPcco().getId()
            ||
            pccoCourant.getId() == rBale.getPcco().getId()
            && tauxCourant!=null
            && rBale.getTauxCcf()!=null
            && rBale.getPartenaire()==null
            && rBale.getTauxCcf()!=tauxCourant
        )      
    )
)


Tks for all your answers

I just transform my condition in several if and it seems to work perfectly ;)

So yes my first condition was too hard for one single line "if" condition ^^

Tks again

I must use this (rBale.getTauxCcf().compareTo(tauxCourant))!= 0 instead of rBale.getTauxCcf() != tauxCourant. They are both of type Double and not double.I thinks that's a common mistake so tks for putting me on the wright track.

Regards,

Cytemax

0

精彩评论

暂无评论...
验证码 换一张
取 消