I currently have a disagreement going on with my 2nd year JAVA professor that I'm hoping y'all could help solve:
The code we started with was this:
public T peek()
{
if (isEmpty())
.........
}
public boolean isEmpty()
{
return topIndex<0;
}
And she wants us to remove the isEmpty()
reference and place its code directly into the if
statement (i.e. change the peek method contents to:
if(topIndex<0).......
) to "Make the code more efficient". I have argued that a) the runtime/compile time optimizer would most likely have inlined
the isEmpty() call, b)
even if it didn't, the 5-10 machine operations would be negligible in nearly every situation, and c) its just bad style because it makes the program less readable and less changeable.
So, I guess my question is: Is there any runtime efficiency gained by inlineing logic as opposed to just calling a method? I have tried simple profiling techniques (aka long loop and a stopwatch) but tests have been inconclusive.
EDIT:
Thank you everyone for the responses! I appreciate you all taking the time. Also, I appreciate those of you who commented on the pragmatism of arguing with my professor and especially doing so without data. @Mike Dunlavey I appreciate your insight as a former professor and your advice on the appropriate coding sequence. @ya_pulser I especially appreciate the profiling advice and links you took the time to share.