Will System.assert in Apex get deleted automatically when moved to production?

Am I free to use System.assert for documentation purposes in my code? I mean will the lines with System.assert be deleted automatically or maybe will be changed in some other way so that not to affect performance?

  • 1
    Your code will not be adjusted. I am afraid this may be a xyproblem.info. Can you explain more in details why do you need to use them in you code? Why do you think that it will affect performance? – kurunve Sep 18 '18 at 8:13
  • @kurunve, no it is definitely not an x-y problem. System.assert helps document the code, I can show to the reader of my code what invariants I am expecting to be hold in different places of the code. For example, by writing System.assert(list.size()==1) I am telling to the reader that the list`s size is always expected to be one here. When I will include a lot of the statements in my code they will require their own execution time, so that affects performance (or if I will include System.debug in loops. – hellohowdoyoudo Sep 18 '18 at 8:22
  • By writing System.assert(list.size()==1) you will tell apex compiler to stop execution in case if your list size is not equal to 1. As soon as you get to the first false, a fatal error is returned that causes code execution to halt. About performance -- due to ad hoc testing, approx 200 000 asserts cause 0.7 sec of CPU time, not that much to worry gist.github.com/kurunve/dd2bf5f5f20d979e0e56e257bf8d79f3 – kurunve Sep 18 '18 at 8:56

No, they won't be adjusted or removed from your code. You need to handle them.

And if due to data or any other condition if the assert didn't match then your end user will get an exception as well.

You should use comments for the documentation purpose if that's your goal. You can update your comments with date to mention all the changes which you have done in your code with what is the initial functionality and what is the end outcome expected.

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.