Another Salesforce blog!!!

Salesforce, Apex

!= null or !isEmpty() ?

Is this in regards to String? If so, I usually use String.isBlank( stringToCheck ). This checks for null, empty and all whitespace.

If this is in regards to collections, you might have to check both:

if ( myList != null && !myList.isEmpty() ) …

If you know that your collection is not null (because you allocated it, or it’s the result of a query), then I prefer !.isEmpty() over .size() > 0:


if ( !myList.isEmpty() ) …

Not this:

if ( myList.size() > 0 ) …

Leave a Reply

Your email address will not be published. Required fields are marked *