Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apex] Add new rule: OperationWithHighCostInLoop #4677

Merged

Conversation

tprouvot
Copy link
Contributor

@tprouvot tprouvot commented Sep 14, 2023

Describe the PR

Methods Schema.getGlobalDescribe() or Schema.describeSObjects should be called once per an apex transaction and should never be called inside a loop.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@tprouvot
Copy link
Contributor Author

@adangel should I create this rule also on a 6.XX version or the 7 is the only one used now ?
Have a good day !

@ghost
Copy link

ghost commented Sep 14, 2023

1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 105830 violations, 0 errors and 1 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact

Generated by 🚫 Danger

@tprouvot
Copy link
Contributor Author

Hi @adangel @rsoesemann,

Just wanted to know if you could have a look on this PR ?

Have a good day !

@adangel adangel self-requested a review October 11, 2023 20:42
@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Oct 18, 2023
@adangel adangel changed the title [apex] Add apex new rule : OperationWithHighCostInLoop [apex] Add new rule: OperationWithHighCostInLoop Oct 18, 2023
Copy link
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think we need a bit more test cases and improve the rule description a bit.

I think, the rule intention is logical IMHO - these method calls should always be used outside of the loop. However, I'm unsure about the real-world practical performance impact: The official apex documentation doesn't mention performance at all - and honestly, I would expect that e.g. Schema.getGlobalDescribes() is cached by the apex implementation anyways.
Do you have any numbers for performance problems created by using these methods extensively (e.g. in a loop)?
Otherwise it would just be an unproven claim and this rule might be better suited for "Best Practices"?

@tprouvot
Copy link
Contributor Author

Thanks for the PR!

I think we need a bit more test cases and improve the rule description a bit.

I think, the rule intention is logical IMHO - these method calls should always be used outside of the loop. However, I'm unsure about the real-world practical performance impact: The official apex documentation doesn't mention performance at all - and honestly, I would expect that e.g. Schema.getGlobalDescribes() is cached by the apex implementation anyways. Do you have any numbers for performance problems created by using these methods extensively (e.g. in a loop)? Otherwise it would just be an unproven claim and this rule might be better suited for "Best Practices"?

Hi @adangel,

You're right, this is not very documented how the usage of this method in loop affects the performance. This thread mention the performances issues and provides measures on it.

In fact, I had the idea of this rule while watching a demo of the Apex Guru (new functionality in Salesforce to provide insights on your implementation and identifying anti pattern usage)

Using this method in loops will be identified as anti-pattern by Apex Guru, so the idea was to be able to identify it before it arrives on the prod environment.

image

As far as I know Schema.getGlobalDescribes() is not cached and there is some custom framework to do so.

I have no problems with tagging this rule in Best practices section instead of performance, you have the final word ! 😄

@adangel adangel added this to the 7.0.0 milestone Oct 28, 2023
@adangel
Copy link
Member

adangel commented Oct 28, 2023

@tprouvot Thanks for the pointers to stackexchange. Then I guess, we keep it in performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:new-rule Proposal to add a new built-in rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[apex] New Rule: OperationWithHighCostInLoop
2 participants