Method that Saves user text messaging draft I am working on saving the text messaging draft of a user. The flow currently works but I am looking to make this block of code easier to read and smaller. Could anyone help me achieve this?

if (!hasDraft) {
    /**
     * This will get triggered when there is no draft
     * and you create one therefore we need a refresh.
     */
    if (!TextUtils.isEmpty(enteredText)) {
        saveDraftAndRefreshList();
    } else {
        /**
         * This should be the happy case.
         * User has just sent a message so no need to refresh the list
         */
        sendMessage();
    }
 /**
  * This else will only be reached if there has been a draft 
  **/
 } else {
     if (TextUtils.isEmpty(enteredText)) {
         /**
          * This will get reached if you had a draft but now removed the text.
          * Thus needing a refresh
          */
         removePreviousDraft();
    } else if (!TextUtils.isEmpty(enteredText)) {
        /**
         * This will get triggered if there was a draft and
         * now they decided to overwrite it.
         */
        overwritePreviousDraft();
    }
}
In both cases (!hasDraft and not !hasDraft) you check whether the enteredText is empty. You can hoist that into a boolean variable outside the if statement.

Also, you are testing if (!hasDraft) but then also doing an else on that condition, which means else (if not)not hasDraft. That can be confusing, so unless some other part of your code flow requires it, I suggest you make your if statement be if (hasDraft) {} else {} so that the implied not from the else matches the !hasDraft case.

boolean noEnteredText = TextUtils.isEmpty(enteredText);

if (hasDraft) {
    if (noEnteredText) {
        removePreviousDraft();
    else {
        overwritePreviousDraft();
    }
} else {
    if (noEnteredText) {
        sendMessage();
    } else {
        saveDraftAndRefreshList();
    }
}


This does not read well, IMO. Again, though, it might be fine in the context of your other code. But I wonder if reversing the sense of noEnteredText to be hasEnteredText would make it better?

boolean hasEnteredText = !TextUtils.isEmpty(enteredText);

if (hasDraft) {
    if (hasEnteredText) {
        overwritePreviousDraft();
    else {
        removePreviousDraft();
    }
} else {
    if (hasEnteredText) {
        saveDraftAndRefreshList();
    } else {
        sendMessage();
    }
}


This still seems off. But I suspect that's probably because I don't know your app. Is it possible you're trying to do too much, and the two decisions should be at two different levels of method call?