DEV Community

Hyesung Lee
Hyesung Lee

Posted on • Updated on

The Semantic Emptiness of Objects in the If Statement

Philosophy

A semantic emptiness is a key concept used to indicate the absence of a value. Even if an object is not actually referenced as null, it can be considered null if it is semantically filled with empty values.

In the beginning there was...

You had to convert an empty value to null to persist it in a database. Also you had to convert null to an empty string to display it in a GUI or something elsewhere like Stream or Writer. If you don't want to use null at all, you had to write a code to getting default value/object. like this:

if (value != null && value.length() <= 0) {
    entity.setValue(null);
} else {
    entity.setValue(value);
}
Enter fullscreen mode Exit fullscreen mode

or

entity.setValue(value == null || value.length() <= 0 ? null : value);
Enter fullscreen mode Exit fullscreen mode

Okay, That's totally fine. But how about this?

entity.setValue(Nullify.of(value));
Enter fullscreen mode Exit fullscreen mode

in some cases, you might want to use empty string("") instead of null:

Nullify.toString(value);
Enter fullscreen mode Exit fullscreen mode

Further, checking for semantically empty

if (collection == null || collection.size() <= 0) {
    collection = Arrays.asList("default_value");
} else {
    boolean isSemanticallyEmpty = true;
    for (String value : collection) {
        if (value != null && value.length() > 0) {
            isSemanticallyEmpty = false;
            break;
        }
    }
    if (isSemanticallyEmpty) {
        collection = Arrays.asList("default_value");
    }
}
Enter fullscreen mode Exit fullscreen mode

above code can also be replaced with:

Nullify.of(collection, Arrays.asList("default_value"));
Enter fullscreen mode Exit fullscreen mode

Conclusion

If you're tired of writing code that checks whether a value is semantically empty or not, you can use Nullify.

GitHub logo silentsoft / nullify

Nullify for null representations and assertions of objects.

Nullify

Maven Central Build Status Quality Gate Status Coverage Hits

Nullify for null representations and assertions of objects.

Philosophy

A semantic emptiness is a key concept used to indicate the absence of a value Even if an object is not actually referenced as null, it can be considered null if it is semantically filled with empty values.

In the beginning there was...

You had to convert an empty value to null to persist it in a database Also you had to convert null to an empty string to display it in a GUI or something elsewhere like Stream or Writer If you don't want to use null at all, you had to write a code to getting default value/object. like this:

if (value != null && value.length() <= 0) {
    entity.setValue(null);
} else {
    entity.setValue(value);
}
Enter fullscreen mode Exit fullscreen mode

or

entity.setValue(value == null || value.length() <= 0 ?
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
cicirello profile image
Vincent A. Cicirello

Nice little utility. Not sure if I'll use it, but nice none-the-less. The very long if-else-if chain of instanceof checks would likely lead me to prefer doing the relevant check directly in most cases. Anyway, I submitted a couple issues on GitHub for improvements to documentation that I think would be helpful for potential users of nullify.

Collapse
 
hyesunglee profile image
Hyesung Lee

Thank you :)