HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

To update or to delete? That is the Query

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theupdatedeletequerythat

Problem

I'm currently developing a method which performs an update of some data; much like the following simplified logic:

boolean updateRequired = (currentValue == null && newValue != null);
boolean deleteRequired = (currentValue != null && newValue == null);

if(updateRequired || deleteRequired) {

    // ...

}


Which probably is fine enough by itself. However, I can't loose the feeling that I'm somehow re-inventing the wheel. The above is just simplified, current/new-Value are strings if that matters.

Is there any way to make this more neat?

Solution

Depending on your use case, I recommend two options. Both of them are based on a separate function for checking things....

At its simplest, have a function:

private static final boolean needToDoSomething(String currentValue, String newValue) {
    return currentValue == null && newValue != null || currentValue != null && newValue == null;
}


Then you can just have:

if (needToDoSomething(currentValue, newValue)) {
    ....
}


If you need to do something more special than just the 'needs modification' test, for example, you need to distinguish inside the if block between these conditions, then I would recommend an Enum with a static method, for example:

public enum EditState {
    ADDED, DELETED, MODIFIED, UNCHANGED;

    public static getState(String currentValue, String newValue) {
        if (currentValue == newValue) {
            // this covers null == null too
            return UNCHANGED;
        }
        if (currentValue != null && newValue == null) {
            return DELETED;
        }
        if (newValue != null && currentValue == null) {
            return ADDED;
        }
        return currentValue.equals(newValue) ? UNCHANGED : MODIFIED;
    }

    public boolean isStateChanging() {
        return this != UNCHANGED;
    }
}


Then, with this Enum class, you can be more specific:

EditState state = EditState.getState(currentValue, newValue);

switch (state) {
    case UNCHANGED :
        .....
    case DELETED :
        .....
    ....
}

Code Snippets

private static final boolean needToDoSomething(String currentValue, String newValue) {
    return currentValue == null && newValue != null || currentValue != null && newValue == null;
}
if (needToDoSomething(currentValue, newValue)) {
    ....
}
public enum EditState {
    ADDED, DELETED, MODIFIED, UNCHANGED;

    public static getState(String currentValue, String newValue) {
        if (currentValue == newValue) {
            // this covers null == null too
            return UNCHANGED;
        }
        if (currentValue != null && newValue == null) {
            return DELETED;
        }
        if (newValue != null && currentValue == null) {
            return ADDED;
        }
        return currentValue.equals(newValue) ? UNCHANGED : MODIFIED;
    }


    public boolean isStateChanging() {
        return this != UNCHANGED;
    }
}
EditState state = EditState.getState(currentValue, newValue);

switch (state) {
    case UNCHANGED :
        .....
    case DELETED :
        .....
    ....
}

Context

StackExchange Code Review Q#44370, answer score: 10

Revisions (0)

No revisions yet.