0

I'm a little bit confused on what is the right way to use setters.

Is the preferable method to create a setters with only 1 parameter of the same object type like this one ?

public void setWebsite(String website) {
    if(website ==null){
        this.website = "";
    }else {
        this.website = website;

    }
}

But i have 2 setters where i'm doubting about

public void setAddressClientList(List<AddressClient> addressClientList,Client client) {
    //Here we add the customer to the address

    if (!addressClientList.isEmpty()) {

        for (AddressClient addressClient : client.getAddressClientList())
        {
            addressClient.setClient(this);
            this.addressClientList.add(addressClient);
        }

    }
}

and

public void setProfessional(String companyName,String vatNumber ) {

    this.professional = !(companyName == null || vatNumber == null);

}

this is the constructor

public Client(Client client,Company company,Client lastInsertClient) throws ClientException {


    setCompany(company);
    setActive(true);
    setCustomField(client.customField);
    setWebsite(client.website);
    setVatNumber(client.vatNumber);
    setPhoneNumber(client.phoneNumber);
    setCurrency("notImplementedYet");
    setFaxNumber(client.faxNumber);
    setCompanyName(client.companyName);
    setSalutation(client.salutation);
    setLastName(client.lastName);
    setEmail(client.email);
    setFirstName(client.firstName);



    setClientNumber(lastInsertClient);
    setProfessional(client.companyName,client.vatNumber);
    setAddressClientList(client.addressClientList,client);


}

Can someone explain if this what the best way to use the setters. And if the last 2 setters are not correct what would you suggest ?

5
  • Whichever best fits your scenario. You are over thinking it Commented Mar 2, 2016 at 17:44
  • 2
    Generally you get/set s single value at a time Commented Mar 2, 2016 at 17:44
  • 2
    Is client ever used in setAddressClientList? If not, why pass it as a parameter? Commented Mar 2, 2016 at 17:45
  • 1
    setProfessional is a confusing-looking method. It looks like a private method which should be called from setCompanyName(companyName) and setVatNumber(vatNumber) - and then it could maybe rely upon the member variables rather than have them passed explicitly (because it's easy to call setProfessional(vatNumber, companyName) (reversing the parameters) by mistake). Commented Mar 2, 2016 at 17:47
  • 1
    A common programming idiom is to encapsulate the parameters into an object and pass one object to the setter. This is useful for showing the coupling relationship between the parameters (ie. that they belong together) and it removes the need to remember parameter order (in cases when there are multiple parameters of the same type and distinguishing their use is not intuitive: public void A(int r, int g, int b, int h, int s, int v){...} is hard to remember which parameters are which). Commented Mar 2, 2016 at 17:57

1 Answer 1

2

Maybe you shouldn't use setters, maybe you should. Setters are mostly used to set a field of the Object that you want. Usually you set a single value at a time (or get it if that is the case), but there can be cases where values should be manipulated in pairs etc. This is a strictly semantic and almost philosophical question, but if you want to do it according to the best practices that people use, I suggest that you rename the method to something more descriptive to avoid confusion if somebody else works with your code. If this is a solo project, you might as well just comment it properly and be on your merry way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.