简体   繁体   中英

Java If-else programming logic

basically I have made a fix on a bug. The fix has worked but now I am wondering if my logic is flawed. I have pasted my code below.Basically I want to know if the 2 lines marked with <-- have an issue. I have written that if data.length >=0 then it equals true, and also else-if data.length && _routing , then it equals to true as well. Is my logic flawed ? Will my else-if condition ever be met, or will it cause an issue ? I am having some trouble visualizing it and could use some help. If its flawed, how should I go about it ? Thank you

if(data.length > 0) {
  if(data.length >= 0) { //<--
    if (_outStr == null){
      _receivedFileContent = true;
      fileCreate();
    }
    _outStr.write(data);
  } else if (data.length == 0 && _routing) { //<--
    _receivedBytearrayZero = true;
  }
} //added during edit for completeness

One flaw that comes to my mind:

if(data.length > 0) {
  if(data.length >= 0) {
   ...
  } else if (data.length == 0 && _routing) {
   ...
  }
}

The else-if branch will never be taken since data.length is already confirmed to be > 0 when that statement is reached.

Instead you'll probably get rid of the data.length >= 0 condition as that looks superfluous:

if(data.length > 0) {
  ... //e.g. write to stream
} else if (data.length == 0 && _routing) {
  ... //got no data but _routing is true so do whatever is appropriate
}

I'd also refactor this part:

if (_outStr == null){
  _receivedFileContent = true;
  fileCreate();
}
_outStr.write(data);

The problem here is that first you check whether _outStr is null and do some initialization in that case. I assume that fileCreate() will initialize _outStr and thus you won't get a NPE in the last line but that's hard to read and requires assumptions or having to look elsewhere which makes the code more fragile.

Without knowing what fileCreate(); is doing I'd prefer something like _outStr = fileCreate(); . That way you at least see that the stream is being initialized if it was null before. Probably you might also want to put that entire block into a separate method like writeToStream(data) and handle the checks inside:

if(data.length >= 0) {
  writeToStream(data); //takes care of initializing the stream if necessary 
}

Thinking in terms of invariants is a great tool here.

if (data.length >= 0) {
     ...block-1...
} else 
// invariant at this point: data.length < 0
if (data.length == 0 && _routing) {
     ...block-2...
}

Clearly the else if branch cannot be entered.

But this only analyzes the internal if-block; you also have a surrounding if:

if (data.length > 0) {
   //invariant at this point: data.length > 0
   if (data.length >= 0) { // implied by the invariant, collapses to true
      ...block-1...
   } else if (data.length == 0 && ...) { // false by invariant, unreachable
      ...block-2...
 }

Simplification by eliminating redundant checks and unreachable code gives us this:

if (data.length > 0) {
  ...block-1...
}

This code will have the same behavior as the code you posted.

if(data.length>0)   ---------(1)
 {
      if(data.length>=0)      ---------(2)
      {
            ...
      }else (data.length==0)&&(routing))    -----(3)
      {
      }
}

The else part of the inner if statement (statement labelled 3) won't be encountered since the outer if (statement labelled 1) itself ensures that the data.length is always greater than 0. That means, there is no need for an inner else condition that checks for the condition data.length=0. If the else condition was an OR operator; there were chances for that to be executed. But, since its an AND operator and one is always FALSE; this means that , it will never be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM