Skip to content

[apex] Promote usage of consistent getDescribe() info #3532

@jonathanwiesel

Description

@jonathanwiesel

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:

  • Should this rule be customizable via 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions