How to refactor this code so that it is clearer and more beautiful, can it be done or is it the best option

AsyncResponse asyncResponse = waiters.remove(userData.getClientId()); if (userData.isManagerEntered()) { response[0] = getPersonEntered(userData); asyncResponse.resume(response[0]); userData.setManagerEntered(false); } else if (userData.isMessageReceve()) { response[0] = getChatReceived(userData); asyncResponse.resume(response[0]); } else if (userData.isDisconnected()) { response[0] = getCallDisconnected(userData); userData.setDisconnected(false); System.out.println("isdisconnect"); asyncResponse.resume(response[0]); } 

    1 answer 1

    You can add constants and replace with the following form:

     switch (userData.status) { case UserData.STATUS_DISCONNECTED: // isDisconnected code break; case UserData.STATUS_MANAGER_ENTERED: // ... break; default: Log.d(TAG, "не обработанный статус") } 

    .resume() for condition


    In the end, something like this should come out:

     AsyncResponse asyncResponse = waiters.remove(userData.getClientId()); switch (userData.status) { case UserDate.STATUS_MANAGER_ENTERED: response[0] = getPersonEntered(userData); userData.setManagerEntered(false); break; case UserData.STATUS_MESSAGE_RECEVE: response[0] = getChatReceived(userData); break; case UserData.STATUS_DISCONNECTED: response[0] = getCallDisconnected(userData); userData.setDisconnected(false); System.out.println("isdisconnect"); break; default: System.out.println("Untreated status: " + userData.status); } asyncResponse.resume(response[0]); 

    src / com / example / UserData

     // public class UserData extends otherUserData { public class UserData { private int _i = 0; static final int STATUS_DISCONNECTED = _i++; static final int STATUS_MANAGER_ENTERED = _i++; static final int STATUS_MESSAGE_RECEVE = _i++ private int status; public UserData() { // Обработка объекта } } 

    Java is generally a language in which you can't do without your own classes. It seems that userData is already some kind of class in which there is a status and this class could be expanded and created its own class for convenience.

    Here I am to what.


     static final enum Status { DISCONNECTED, MANAGER_ENTERED, MESSAGE_RECEVE } 

    while Status is already an object, not an int.

    http://www.quizful.net/post/java_enums

    Learn more about enum in java

    • how do I add constants and check through status, what do you mean by constants - J Mas
    • supplemented the answer. - Pleshevskiy
    • Is it possible to use enum somehow? - J Mas
    • In Java enum, alas, there is no such as in the same c ++. The code will be released more than I described above. But as an option ... now add code - Pleshevskiy
    • You mean that the code will be more, and whether it will be more readable - J Mas