EagerlyLoadedDescribeSObjectResult - lpohl-Reply/pmd-github-action GitHub Wiki
DescribeSObjectResult could be being loaded eagerly with all child relationships.
This rule finds DescribeSObjectResults which could have been loaded eagerly via SObjectType.getDescribe().
When using SObjectType.getDescribe() or Schema.describeSObjects() without supplying a SObjectDescribeOptions,
implicitly it will be using SObjectDescribeOptions.DEFAULT and then all
child relationships will be loaded eagerly regardless whether this information is needed or not.
This has a potential negative performance impact. Instead SObjectType.getDescribe(options)
or Schema.describeSObjects(SObjectTypes, options)
should be used and a SObjectDescribeOptions should be supplied. By using
SObjectDescribeOptions.DEFERRED the describe attributes will be lazily initialized at first use.
Lazy loading DescribeSObjectResult on picklist fields is not always recommended. The lazy loaded
describe objects might not be 100% accurate. It might be safer to explicitly use
SObjectDescribeOptions.FULL in such a case. The same applies when you need the same DescribeSObjectResult
to be consistent across different contexts and API versions.
Properties:
-
noDefault: The behavior ofSObjectDescribeOptions.DEFAULTchanges from API Version 43 to 44: With API Version 43, the attributes are loaded eagerly. With API Version 44, they are loaded lazily. Simply usingSObjectDescribeOptions.DEFAULTdoesn't automatically make use of lazy loading. (unless "Use Improved Schema Caching" critical update is applied,SObjectDescribeOptions.DEFAULTdoes fallback to lazy loading) With this property enabled, such usages are found. You might ignore this, if you can make sure, that you don't run a mix of API Versions.
4 (was 3)
public class Foo {
public static void bar(List<Account> accounts) {
if (Account.SObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).isCreateable()) {
insert accounts;
}
}
}