When to create a local variable and when to call the method of objects?

Here is the script.

Object A has a method that receives the object. There are 2 methods. Both do essentially the same thing.
randomCheck1 () makes a call each time to the isValid () method.
randomCheck2 () makes a call once and every time uses a local variable.

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 that states that if a method needs to be called more than once, then a local variable must be created?

+4
source share
4 answers

You basically have two questions:

1) ?

Ans - . , isValid(). , , , , , , .

, 1 , 3 3 . 2 .

isValid() , .

2) , , , ?

Ans - , , , .

, isValid() , . , , , 1 , , . .

0

, . - , .

, jit "" isValid, , , , , -, . , getInfo isValid , , , .

0

check2 - , isValid() funtion .

0

-, :

  • obj.getInfo(). isValid() obj.getInfo() (: isValid() db ).
  • obj.getInfo(). isValid() , , ,
  • If you consider performance , then it depends on how long the isValid () method is used. If it takes very little time, it will not make any difference.
0
source

Source: https://habr.com/ru/post/1670743/


All Articles