25T2 Week 10 🥳
Friday 1-2pm (F13A)
MyExperience ->
Please fill it in (5 mins)
Finding patterns
Strategy
Composite
Observer
Decorator
Observer
Abstract Factory
Decorator
Code and Design Smells
Mark, Bill and Jeff are working on a PetShop application. The PetShop has functionality to feed, clean and exercise different types of animals.
Mark notices that each time he adds a new species of animal to his system, he also has to rewrite all the methods in the PetShop so it can take care of the new animal.
What code or design smell is present here?
public class Person {
private String firstName;
private String lastName;
private int age;
private int birthDay;
private int birthMonth;
private int birthYear;
private String streetAddress;
private String suburb;
private String city;
private String country;
private int postcode;
public Person(String firstName, String lastName,
int age, int birthDay, int birthMonth,
int birthYear, String streetAddress,
String suburb, String city, String country,
int postcode) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.birthDay = birthDay;
this.birthMonth = birthMonth;
this.birthYear = birthYear;
this.streetAddress = streetAddress;
this.suburb = suburb;
this.city = city;
this.country = country;
this.postcode = postcode;
}
// Some various methods below...
}
What code or design smell is present here?
Data clumps & long parameter list
How would you refactor to fix this?
If some of the input was "optional" builder pattern may be useful here
public class MathLibrary {
List<Book> books;
int sumTitles {
int total = 0
for (Book b : books) {
total += b.title.titleLength;
}
return total;
}
}
public class Book {
Title title; // Our system just models books
// as titles (content doesn't matter)
}
public class Title {
int titleLength;
int getTitleLength() {
return titleLength;
}
void setTitleLength(int tL) {
titleLength = tL;
}
}
What code or design smell is present here?
How would you refactor to fix this?
How do these code smells cause problems when developing code?
How fast does it take for a new developer to understand what is happening? Is behaviour predictable, reasonable and uniform?
Is a code smell always emblematic of a design problem?
No