Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Nested If or single if

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();
like image 539
Deva Avatar asked Sep 18 '15 02:09

Deva


People also ask

Can you have 2 if statements in Java?

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.

How many if statements can you nest in Java?

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.

What is the difference between if and nested IF?

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.

Why we use nested if-else in Java?

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.


2 Answers

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.

like image 89
shihpeng Avatar answered Oct 12 '22 18:10

shihpeng


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.

like image 23
Sergey Kalinichenko Avatar answered Oct 12 '22 18:10

Sergey Kalinichenko