Here is a scenario.
Object A has a method which receives an object.
There are 2 methods. Both are doing essentially the same thing.
randomCheck1() makes a call each time to the isValid() method.
randomCheck2() makes a call once then uses the local variable each time.
Class A
{
randomCheck1(myObject obj)
{
if (obj.getInfo().isValid())
{
:
}
// Do some more work.
if (obj.getInfo().isValid())
{
:
}
// Do some more work.
if (obj.getInfo().isValid())
{
:
}
}
randomCheck2(myObject obj)
{
boolean isValidCheck = obj.getInfo().isValid();
if (isValidCheck)
{
:
}
// Do some more work.
if (isValidCheck)
{
:
}
// Do some more work.
if (isValidCheck)
{
:
}
}
}
Is there a performance difference between the two?
Is there a coding standard which states that if a method needs to be called more than once then a local variable should be created?