mardi 12 février 2019

Is it a bad practice to use a set inside get?

Hi I am using the MVVM pattern in an android project, and for the ViewModel have code like this:

public class LoginViewModel extends ViewModel {

    public MutableLiveData<User> user = new MutableLiveData<>();
    public MutableLiveData<String> email = new MutableLiveData<>();
    public MutableLiveData<String> password = new MutableLiveData<>();
    public MutableLiveData<Boolean> emailError = new MutableLiveData<>();
    public MutableLiveData<Boolean> register = new MutableLiveData<>();
    private LoginRespository loginRespository = new LoginRespository();
    private MutableLiveData<Boolean> enable = new MutableLiveData<>();

    public LoginViewModel() {
    }

    public void login() {
        ...
    }

    public void startRegister() {
        ...
    }

    private String getEmailValue() {
        if (email.getValue() == null) {
            email.setValue("");
        }

        return email.getValue();
    }
....
}

In the office, we have a discussion about that use the set inside the get is a bad practice, but I think that this is not a bad practice because java allows nulls, and I want not to get a null value when calling the getEmailValue() I think that the concept of encapsulation is for these cases.

The question is if really this can be a bad practice, or not?

Thanks

Aucun commentaire:

Enregistrer un commentaire