I have a basic doubt regarding the execution of the following code block (Sample):
String version = computer.getSoundcard().getUSB().getVersion();
Which might throw NullPointerException
if Soundcard
isn't there.
So I have ,
Option 1 :
if(computer!=null &&
computer.getSoundCard() !=null &&
computer.getSoundCard().getUSB()!=null) {
version = computer.getSoundcard().getUSB().getVersion();
}
Option 2 :
if(computer !=null){
SoundCard sc = computer.getSoundCard();
if(sc!=null){
USB usb = sc.getUSB();
if(usb!=null){
version = usb.getVersion();
}
}
}
As per my understanding the Option 1 will have extra overhead as it has to evaluate the same expression multiple times like computer.getSoundCard()
3 times, computer.getSoundCard().getUSB()
2 times.
Is my understanding correct ?
EDIT 1: Changed Option 2 from
version = computer.getSoundcard().getUSB().getVersion();
Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.
You can quadruply nest, or even more. This is an example of a triply nested if. if ( num > 0 ) if ( num < 10 ) if ( num % 2 == 0 ) System. out.
One IF function has one test and two possible outcomes, TRUE or FALSE. Nested IF functions, meaning one IF function inside of another, allows you to test multiple criteria and increases the number of possible outcomes.
It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
A better approach is to extract this USB-version-getting code into another single method, say getComputerUsbVersion()
, then flatten the super long if
or the nested if-else
block into several simple if
blocks:
public String getComputerUsbVersion(Computer computer) {
if (computer == null) return null;
SoundCard soundCard = computer.getSoundCard();
if (soundCard == null) return null;
USB usb = soundCard.getUSB()
if (usb == null) return null;
return usb.getVersion();
}
As you can see, the code is much cleaner and easy to understand, the super long if
condition or the nested if-else
block is also avoided. You can even add more condition checking code to this method later on very easily.
As per my understanding the Option 1 will have extra overhead as it has to evaluate the same expression multiple times
Yes, these calls would be made multiple times. However, you can shorten it if you make assignments as part of your conditional, like this:
SoundCard sc;
USB usb;
if(computer != null && (sc = computer.getSoundCard()) != null && (usb = sc.getUSB()) != null) {
version = usb.getVersion();
}
Note that references to sc
and usb
inside the expression and inside the conditional are safe, because &&
evaluation is guaranteed to stop upon reaching the first false
in the chain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With