Question

While i was writing a BST find function:

public static boolean find(Node root, int value){
    if(root == null){
        return false;
    }
    Node iter = root;

    while(iter != null){
    .....
    }
    return false;
}

will the compiler remove

if(root == null){
   return false;
}

or not. Since i am checking for the condition anyway in the while loop.

Thanks.

ps. i just realized, i am passing by value so no need for iter Node.. too much C

Was it helpful?

Solution

It depends on your optimizations. If you have full optimizations and the compiler can prove that removing the initial if will not change the output of the function, then yes it may remove it.

In the exact code sample you provided, I would venture that the compiler could indeed optimize the first if away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top