Album Cover

Code Smells Java

Close Preview
In the Customer class below, the method getMobilePhoneNumber() provides a North American-formatted mobile Phone number:
public class Phone {
   private final String unformattedNumber;
public Phone(String unformattedNumber) { this.unformattedNumber = unformattedNumber; }
public String getAreaCode() { return unformattedNumber.substring(0,3); }
public String getPrefix() { return unformattedNumber.substring(3,6); }
public String getNumber() { return unformattedNumber.substring(6,10); } }
public class Customer… private Phone mobilePhone;
public String getMobilePhoneNumber() { return "(" + mobilePhone.getAreaCode() + ") " + mobilePhone.getPrefix() + "-" + mobilePhone.getNumber(); }
See how Customer reaches into Phone’s data to format the number? Feature Envy often shows up as a misplaced responsibility.
public class Phone {
   private final String unformattedNumber;
public Phone(String unformattedNumber) { this.unformattedNumber = unformattedNumber; }
private String getAreaCode() { return unformattedNumber.substring(0,3); }
private String getPrefix() { return unformattedNumber.substring(3,6); }
private String getNumber() { return unformattedNumber.substring(6,10); }
public String toFormattedString() { return "(" + getAreaCode() + ") " + getPrefix() + "-" + getNumber(); } }
public class Customer… private Phone mobilePhone;
public String getMobilePhoneNumber() { return mobilePhone.toFormattedString(); }
Now Customer relies on Phone to do the formatting.
(Reading...)
| Tweet...
Page rating: | Rate this Page