Reputation: 4019
I have the following code in an equals method.
public boolean equals(Object o){
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Vertex)) return false;
return ((Vertex) o).label().equals(label);
}
My IDE highlights the if statement and wants me to basically do this
public boolean equals(Object o){
return (o != null) && ((o==this) || ((o instanceof Vertex) && ((Vertex) o).label().equals(label);
}
I've been told that the compiler is generally smart enough to optimize and that in general one should code for readability. So, clearly the second code sample is not as easy to read as the first. Is my IDE just being annoying or is there some actual performance merit to doing it that way?
Upvotes: 2
Views: 2822
Reputation: 18028
Why not test the performance your self? Here is a sample:
public static long timeIt(Runnable runnable) {
long start = System.nanoTime();
runnable.run();
long end = System.nanoTime();
return end - start;
}
will time the run method for you. So you can call it like this:
timeIt(new Runnable() { public void run() { OPERATION_TO_BE_TIMED; }})
Upvotes: 0
Reputation: 1053
The performance benefit of the version suggested by your IDE is virtually none. Furthermore, you are right that JVM (not only compiler) can do a lot of optimizations.
As others pointed out, try to reach for readability. The code you write is read by human beings and compiled, modified and optimized by Java compiler and virtual machine.
Btw. a short hint: your code can be optimized a little more like this (leaving out the condition for equality to null
, since o
is guaranteed to be instance of Vertex after the second condition):
public boolean equals(Object o){
if (o == this) return true;
if (!(o instanceof Vertex)) return false;
return ((Vertex) o).label().equals(label);
}
Upvotes: 1
Reputation: 41858
The benefit to doing it this way is that if you inline the code then you can assign it easily to a boolean, but you could also use a ternary operator to make it more readable, which will be more readable than the optimized version, but would have the same functionality, and may help you to simplify coding later if you learn how to do it.
public boolean equals(Object o){
return o == null || !(o instance of Vertex) ? false :
o == this ? true :
((Vertex) o).label().equals(label);
}
This should still be readable.
I haven't tried to compile this, some extra parenthesis may be needed, but I am going for a concept not for something you can copy & paste.
Upvotes: 0
Reputation: 53516
First, only optimize if you know it's a bottleneck, else code for readability.
You can check the byte code to see but I suspect they are pretty close if not exactly the same. Even if there are slight differences in bytecode, I've seen the JIT compiler optimize stuff down to where there is no difference. You can always do performance tests to be sure.
Upvotes: 2
Reputation: 10254
Don't try to out smart the compiler. You are writing code for a person to read. Let the compiler worry about converting it to machine readable format.
Upvotes: 0
Reputation: 88378
Most IDEs are configurable and allow you to specify the kinds of style warnings it outputs. You are correct that there is no benefit to doing what the compiler suggests in terms of the code that is generated. Compilers should indeed be able to optimize these kinds of things.
Upvotes: 1