Code Smell of the year

Data class

The next code smell in your code could be on data classes. Data classes have fields with getters and setters, but no behaviour.

One of the goals of object oriented design is to put the behaviour where the data is. That low couples your components and supports “Tell, don’t ask”.

So put methods that act on fields in the same class that contain those fields.

public class CustomerSummaryView {
    private Customer customer;

    public CustomerSummaryView(Customer customer){
        this.customer = customer;
    }

    public String getCustomerSummary(){
        Address address = customer.getAddress();
        return customer.getTitle() + " " + customer.getFirstName() + " " + customer.getLastName() + ", " + 
                address.getCity() + ", " + address.getPostcode() + ", " + address.getCountry();
    }
}


public class Customer {

    private String title;
    private String firstName;
    private String lastName;
    private Address address;

    //getters and setters    
}



public class Address {
    
    private String city;
    private String postcode;
    private String country;

    //getters and setters

}


You can fork it here from Github.

Hint: Pay attention to possible refactoring steps in your test suite

This is step three of the second part of the Refactoring kickstart beginner series. Here is step 4.