Proposed Rule Name:
GetDescribeShouldUseSObjectDescribeOptions
Proposed Category:
Performance
Description:
In versions earlier than v43 calling the .getDescribe()
method on an SObjectType eager-loads all describe elements which may not even be required for the implementation, therefore wasting resources on the describe call and affecting performance.
Using the .getDescribe(options)
method ensures developers explicitly set their intentions on what they need to both prevent from eager-loading when not required to avoid performance issues or on the contrary eager-load to guarantee consistency whenever the instance is called or passed to.
More info mentioned here from @capeterson.
Code Sample:
Should be flagged
public class Foo { public void bar(List<Account> accounts) { if (Account.SObjectType.getDescribe().isCreateable()) { insert accounts; } } }
This would be correct
public class Foo { public void bar(List<Account> accounts) { if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).isCreateable() && Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFAULT).isUpdateable()) { upsert accounts; } } }
Possible Properties:
One that could be given is whether to allow or not the SObjectDescribeOptions.DEFAULT
option, since this will depend on the caller API version it may also introduce the same problems as just leaving any options out unless the use case justifies it.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4