3

When accessing Object properties like 'Account.Name' multiple times throughout the code, is it better to create a variable and assign the property value to the variable once or is it fine to continuously reference the same property throughout the code? See example for more detail:

if(Account.Name.equals('Ben')) 
    Account.Status = 'Open';
else if(Account.Name.equals('Sim'))
    Account.Status = 'Closed';

Case.Name = Account.Name;
Opportunity.Newest = Account.Name;

As you can see above, the same 'Account.Name' property is accessed multiple times. Is it more efficient to do something like:

String accountName = Account.Name;

if(accountName.equals('Ben')) 
    Account.Status = 'Open';
else if(accountName.equals('Sim'))
    Account.Status = 'Closed';

Case.Name = accountName;
Opportunity.Newest = accountName;
  • I do not see a reason to be more efficient to create a variable. Thinking, could be less, as you are creating more one variable. – m Peixoto Dec 21 '18 at 10:08
5

It is (or, at least, used to be) faster to use a variable if you're going to use the field lots of times. See this video for lots of experimentation:

https://www.youtube.com/watch?v=w6QnalRWlEE

Measured time on Salesforce will change as the compiler changes, and server load changes. So, unless you measure carefully, you might not even be able to see the difference.

Warning: opinion coming...

I would always treat code cleanliness (is it easy to read?) as more important than performance until you run into an actual performance problem. To quote Knuth:

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

However, introducing an extra variable can help with cleanliness because it gives you a chance to give that variable a descriptive name which makes the code easier to follow.

Personally, that's why I'd use the extra variable.

  • +1 While caching a field value is rarely worth it from a performance perspective, developers should be mindful of other types of variables. For example, if you get a value from a map and you plan on using it a lot, you can save significant CPU time. In all other cases, it's more of a legibility issue. – sfdcfox Dec 21 '18 at 16:13
0

There could be minor efficiency improvement, but IMO more of coding cleaness as Aidan mentioned already.

This is what I will code below. If more conditions are coming, a switch statement might fit better, or using base class and inherited class for polymorphism to scape from condition check.

String accountName = Account.Name;


if(accountName == 'Ben'){ 
    Account.Status = 'Open';
} else if(accountName == 'Sim'){
    Account.Status = 'Closed';
}

Case.Name = accountName;
Opportunity.Newest = accountName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.