141

In parts of my code I have DML statements with an if around to only do the DML if the list is not empty:

if( !recordsToUpdate.isEmpty() ) {
   update recordsToUpdate;
}

Couldn't I just write?

update recordsToUpdate;

Or would I provoke errors or needlessly consumed limits by doing that?

2 Answers 2

129

Good question, I've never considered it before...

i've just assumed the platform will play fair and I'm pleased to find it does!

System.debug('Before DmlStatements : ' + Limits.getLimitDmlStatements());
System.debug('Before DmlRows : ' + Limits.getDmlRows());
insert new List<Account>();
System.debug('After DmlStatements : ' + Limits.getLimitDmlStatements());
System.debug('After DmlRows : ' + Limits.getDmlRows());

Results in a successful completion and confirmation of no governor usage.

09:00:14.185 (5185191000)|USER_DEBUG|[1]|DEBUG|Before DmlStatements : 150

09:00:14.185 (5185303000)|USER_DEBUG|[2]|DEBUG|Before DmlRows : 0

09:00:14.185 (5185553000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|()

09:00:14.185 (5185606000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|()

09:00:14.185 (5185933000)|USER_DEBUG|[4]|DEBUG|After DmlStatements : 150

09:00:14.186 (5186055000)|USER_DEBUG|[5]|DEBUG|After DmlRows : 0

3
  • 2
    That is a great question. Like you, I had never considered it before and always just threw in the check for non empty lists. Very interesting find, so in theory are we just wasting a line of code by checking the list isn't empty? Is there any other advantage to checking before the insert? Commented Nov 8, 2013 at 12:25
  • 2
    None that I can think, i think its a reasonable contractual assumption that if a method (or operation in this case) takes a collection, it should a) not fail and b) use very minimal resources to execute if the list is empty. It seems in this case the SF developers are complying with this design assumption. :) Commented Nov 8, 2013 at 13:01
  • 25
    Note: Historically, empty lists did consume governor limits, which is why so many developers used the if-not-empty syntax. After this was fixed, a lot of old code, blog posts, etc were never updated. As a very small aside, the DML statement itself performs a if-not-empty check, which means that if a developer does so, the effect is the list is checked twice to see if it's empty, wasting a few milliseconds of CPU time. It's not a significant amount, but the point is that writing if-not-empty results in wasted code and CPU time. It has no upside.
    – sfdcfox
    Commented Jul 11, 2019 at 20:11
12

When I look at the results of the following coding, I would still use if (!list.isEmpty()) { DML list; }

List<Account> accs = new List<Account>();
System.debug(Limits.getCpuTime()); // 1
// loop 1
for (Integer i = 0; i < 100000; i++) {
    insert accs;
}
System.debug(Limits.getCpuTime()); // 2039
// loop 2
for (Integer i = 0; i < 100000; i++) {
    if (!accs.isEmpty()) {
        insert accs;
    }
}
System.debug(Limits.getCpuTime()); // 2291

The order of Loop 1 and Loop 2 does not matter. The loop using if (!list.isEmpty()) { DML list; } is always faster, almost by a factor of 10 in this example. So the result is different than sfdcfox says in his comment.

Of course this is a weak argument, since we have (currently) only 150 DML statements available anyway.

But another point is that you can immediately tell from the code coverage whether the command was executed or not.

2
  • Interesting. I've never seen results like this; it might have been a red herring. I've always used the "don't ask first" technique as a performance matter.
    – sfdcfox
    Commented Mar 4, 2021 at 14:18
  • Funny, I'm always using the "ask first" technique to save unnecessary execution steps. Essentially, I guess, it depends on "what comes next" :)
    – loeachim
    Commented Mar 5, 2021 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .