4

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?

Winter
  • 3,894
  • 7
  • 24
  • 56
Unhandled Exception
  • 1,427
  • 14
  • 30
  • related: https://stackoverflow.com/questions/39888446/is-it-better-to-call-a-method-on-a-variable-or-chain-it-to-the-constructor –  Feb 24 '17 at 12:55
  • Thanks. Lots of great feedback. I believe the key here is whether or not the underlying value could possibly change. – Unhandled Exception Feb 24 '17 at 13:53

4 Answers4

0

Kind of matter of taste when you have just one value to test against. If you have to take into account for multiple values or to do things consistently in two or more phases, it is better to keep a local variable.

For performance point of view, I guess the jit can "inline" the isValid if it a simple return of a field but this is secondary because compared to execution time dedicated to event handling this is nothing to realy bother. In the opposite direction, if getInfo or isValid takes a lot of time, that is more obvious that you should avoid calling them more than necessary.

Laurent G
  • 397
  • 8
  • 16
0

Random check2 is the best option especially when isValid() funtion does large piece of work in each invocation.

0

Actually it depends on the business scenario also, which one to use :

  • Value obj.getInfo().isValid() or obj.getInfo() is changing during the execution of the method then use the first way (for example: if on calling isValid() it is fetching the value from db each time and is variable).
  • If the value of obj.getInfo().isValid() is constant during the execution of the method use either of the two, though second one is more readable.
  • If you consider the performance point then it depends on how much time the isValid() method is taking. If it is taking very less time then it won't make any difference.
Rohit Gulati
  • 542
  • 3
  • 15
0

You basically have two questions,

1) Is there a performance difference between the two?

Ans - It depends. It mainly depends how heavy your isValid() method is. If your method is doing some heavy computation to check the validity of the object and then give you a result, then obviously it is better to call this method once and then re-use it.

Suppose your method takes 1ms to compute the validity and return you a result, then 3 calls would take 3ms. But you could make a single call and re-use the value and save 2ms.

But if the method isValid() is very simple in terms of computation, then there is no performance difference between the two.

2) 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?

Ans - This is no hard-and-fast rule, but it is always cleaner to call the method once, grab its value and store it in a variable and then re-use it anytime you want.

But keep in mind, that if the isValid() methods result changes with time, then you need to call it multiple time. Suppose, you call it the first time and it gives you true, but you call it after 1 second and it gives you false, then in that case you CANNOT store the value and re-use it as the value changes. You need to call it multiple times.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • Do let me know if there is anything else you need to know. If this answer helps you, please consider upvoting it a well. – Aritra Roy Feb 24 '17 at 14:48