After working on many different code bases in Salesforce, I noticed that almost all developers check if a list is empty before doing a DML in order to avoid the unnecessary DML statement.
Here's an example:
if(!accountList.isEmpty()) {
update accountList;
}
However, I tried running the code below in Execute Anonymous:
Account[] accounts = new Account[]{};
update accounts;
System.debug(Limits.getDmlRows()); // Returns Zero
System.debug(Limits.getDmlStatements()); // Returns Zero
System.debug(Limits.getLimitDmlRows()); // Returns 10,000
System.debug(Limits.getLimitDmlStatements()); // Returns 150
Based on the results, I came to the conclusion that checking if a list is empty or not is unnecessary when it comes to Salesforce limits. It also does not fire any triggers, flows etc. on the Account object.
I guess the question is: Is there any other reasons to do the check before a DML or is this common trap we all fell into?