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 ?
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.